<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Examples</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter 1. Boost.Contract 1.0.0">
<link rel="up" href="../index.html" title="Chapter 1. Boost.Contract 1.0.0">
<link rel="prev" href="extras.html" title="Extras">
<link rel="next" href="../reference.html" title="Reference">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="extras.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../reference.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_contract.examples"></a><a class="link" href="examples.html" title="Examples">Examples</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="examples.html#boost_contract.examples.__n1962___vector__contracts_for_stl_vector_and_comparison_with___n1962___proposed_syntax">[N1962] Vector: Contracts for STL
      vector and comparison with [N1962] proposed
      syntax</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__n1962___circle__subcontracting">[N1962]
      Circle: Subcontracting</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__n1962___factorial__recursion">[N1962]
      Factorial: Recursion</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__n1962___equal__operators">[N1962]
      Equal: Operators</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__n1962___sum__array_parameter">[N1962]
      Sum: Array parameter</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__n1962___square_root__default_parameters_and_comparison_with_d_syntax">[N1962]
      Square Root: Default parameters and comparison with D syntax</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__meyer97___stack4__comparison_with_eiffel_syntax">[Meyer97]
      Stack4: Comparison with Eiffel syntax</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__meyer97___stack3__error_codes_instead_of_preconditions">[Meyer97]
      Stack3: Error codes instead of preconditions</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__mitchell02___name_list__relaxed_subcontracts">[Mitchell02]
      Name List: Relaxed subcontracts</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__mitchell02___dictionary__key_value_map">[Mitchell02]
      Dictionary: Key-value map</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__mitchell02___courier__subcontracting_and_static_class_invariants">[Mitchell02]
      Courier: Subcontracting and static class invariants</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__mitchell02___stack__stack_like_container">[Mitchell02]
      Stack: Stack-like container</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__mitchell02___simple_queue__queue_like_container_and_disable_old_value_copies_for_audit_assertions">[Mitchell02]
      Simple Queue: Queue-like container and disable old value copies for audit assertions</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__mitchell02___customer_manager__contracts_instead_of_defensive_programming">[Mitchell02]
      Customer Manager: Contracts instead of defensive programming</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__mitchell02___observer__pure_virtual_functions">[Mitchell02]
      Observer: Pure virtual functions</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__mitchell02___counter__subcontracting">[Mitchell02]
      Counter: Subcontracting</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__cline90___vector__comparison_with_a___proposal_syntax">[Cline90]
      Vector: Comparison with A++ proposal syntax</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__cline90___stack__stack_like_container">[Cline90]
      Stack: Stack-like container</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__cline90___vector_stack__subcontracting">[Cline90]
      Vector-Stack: Subcontracting</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_contract.examples.__cline90___calendar__a_very_simple_calendar">[Cline90]
      Calendar: A very simple calendar</a></span></dt>
</dl></div>
<p>
      This section lists some examples taken from different sources discussing contract
      programming and implemented here using this library.
    </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
        Some of these examples might be from old code, containing obsolete coding
        practices, not optimized for execution speed, not complete, and they might
        be more relevant in the context of programming languages different from C++.
        Nevertheless, programmers are encouraged to review these examples to see
        a few diverse uses of this library that might be relevant to their needs.
      </p></td></tr>
</table></div>
<p>
      The examples in this sections are taken from the following sources:
    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          <a class="link" href="bibliography.html#N1962_anchor">[N1962]</a>: Examples from a detailed and
          complete proposal to add contract programming to C++11 (unfortunately,
          this proposal was never accepted into the standard).
        </li>
<li class="listitem">
          <a class="link" href="bibliography.html#Meyer97_anchor">[Meyer97]</a>: Examples from the Eiffel
          programming language (reprogrammed here in C++ using this library).
        </li>
<li class="listitem">
          <a class="link" href="bibliography.html#Mitchell02_anchor">[Mitchell02]</a>: Additional examples
          from the Eiffel programming language (reprogrammed here in C++ using this
          library).
        </li>
<li class="listitem">
          <a class="link" href="bibliography.html#Cline90_anchor">[Cline90]</a>: Examples from a very early
          proposal called Annotated C++ (A++) to add contract programming to C++
          (A++ was never implemented or proposed for addition to the standard).
        </li>
</ul></div>
<p>
      The following are some examples of particular interest:
    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          <a class="link" href="examples.html#N1962_vector_anchor">[N1962] Vector</a>: Complete set
          of contracts for <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span></code>, plus a comparison with <a class="link" href="bibliography.html#N1962_anchor">[N1962]</a> syntax.
        </li>
<li class="listitem">
          <a class="link" href="examples.html#N1962_square_root_anchor">[N1962] Square Root</a>: Comparison
          with D syntax.
        </li>
<li class="listitem">
          <a class="link" href="examples.html#Mitchell02_counter_anchor">[Mitchell02] Counter</a>:
          Subcontracting.
        </li>
<li class="listitem">
          <a class="link" href="examples.html#Meyer97_stack4_anchor">[Meyer97] Stack4</a>: Comparison
          with Eiffel syntax.
        </li>
<li class="listitem">
          <a class="link" href="examples.html#Cline90_vector_anchor">[Cline90] Vector</a>: Comparison
          with A++ syntax.
        </li>
</ul></div>
<p>
      Most of the examples listed here use old values and class invariants which
      are instead not supported by <a class="link" href="bibliography.html#P0380_anchor">[P0380]</a>. Therefore,
      there is not meaningful example here that can be directly implemented and compared
      using <a class="link" href="bibliography.html#P0380_anchor">[P0380]</a> syntax.
    </p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__n1962___vector__contracts_for_stl_vector_and_comparison_with___n1962___proposed_syntax"></a><a name="N1962_vector_anchor"></a>[N1962] Vector: Contracts for STL
      vector and comparison with <a class="link" href="bibliography.html#N1962_anchor">[N1962]</a> proposed
      syntax</h3></div></div></div>
<p>
        On compilers that support C++17 <code class="computeroutput"><span class="keyword">if</span>
        <span class="keyword">constexpr</span></code>, the following example using
        this library can be simplified removing <code class="computeroutput"><a class="link" href="../boost/contract/condition_if.html" title="Function template condition_if">boost::contract::condition_if</a></code>
        and related functor templates such as <code class="computeroutput"><span class="identifier">all_of_equal_to</span></code>,
        etc., making it more similar to the pseudo-code on the right-hand side (see
        <a class="link" href="extras.html#boost_contract.extras.assertion_requirements__templates_" title="Assertion Requirements (Templates)">Assertion
        Requirements</a>).
      </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  This library
                </p>
              </th>
<th>
                <p>
                  [N1962] proposal (not accepted in C++) plus C++17 <code class="literal">if constexpr</code>
                </p>
              </th>
</tr></thead>
<tbody><tr>
<td>
                <p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">bind</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">optional</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">algorithm</span><span class="special">/</span><span class="identifier">cxx11</span><span class="special">/</span><span class="identifier">all_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_traits</span><span class="special">/</span><span class="identifier">has_equal_to</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">next_prior</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">functional</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iterator</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">memory</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="comment">// Could be programmed at call site with C++14 generic lambdas.</span>
<span class="keyword">struct</span> <span class="identifier">all_of_equal_to</span> <span class="special">{</span>
    <span class="keyword">typedef</span> <span class="keyword">bool</span> <span class="identifier">result_type</span><span class="special">;</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">InputIter</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
    <span class="identifier">result_type</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">InputIter</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">InputIter</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">algorithm</span><span class="special">::</span><span class="identifier">all_of_equal</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">value</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">InputIter</span><span class="special">&gt;</span>
    <span class="identifier">result_type</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">InputIter</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">InputIter</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">InputIter</span> <span class="identifier">where</span><span class="special">)</span> <span class="special">{</span>
        <span class="keyword">for</span><span class="special">(</span><span class="identifier">InputIter</span> <span class="identifier">i</span> <span class="special">=</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">j</span> <span class="special">=</span> <span class="identifier">where</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">!=</span> <span class="identifier">last</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">,</span> <span class="special">++</span><span class="identifier">j</span><span class="special">)</span> <span class="special">{</span>
            <span class="keyword">if</span><span class="special">(*</span><span class="identifier">i</span> <span class="special">!=</span> <span class="special">*</span><span class="identifier">j</span><span class="special">)</span> <span class="keyword">return</span> <span class="keyword">false</span><span class="special">;</span>
        <span class="special">}</span>
        <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>
    <span class="special">}</span>
<span class="special">};</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Iter</span><span class="special">&gt;</span>
<span class="keyword">bool</span> <span class="identifier">valid</span><span class="special">(</span><span class="identifier">Iter</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">Iter</span> <span class="identifier">last</span><span class="special">);</span> <span class="comment">// Cannot implement in C++ (for axiom only).</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Iter</span><span class="special">&gt;</span>
<span class="keyword">bool</span> <span class="identifier">contained</span><span class="special">(</span><span class="identifier">Iter</span> <span class="identifier">first1</span><span class="special">,</span> <span class="identifier">Iter</span> <span class="identifier">last1</span><span class="special">,</span> <span class="identifier">Iter</span> <span class="identifier">first2</span><span class="special">,</span> <span class="identifier">Iter</span> <span class="identifier">last2</span><span class="special">);</span> <span class="comment">// For axiom.</span>

<span class="comment">// STL vector requires T copyable but not equality comparable.</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Allocator</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">vector</span> <span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">void</span> <span class="identifier">invariant</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">empty</span><span class="special">()</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">));</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">end</span><span class="special">())</span> <span class="special">==</span> <span class="keyword">int</span><span class="special">(</span><span class="identifier">size</span><span class="special">()));</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">rbegin</span><span class="special">(),</span> <span class="identifier">rend</span><span class="special">())</span> <span class="special">==</span> <span class="keyword">int</span><span class="special">(</span><span class="identifier">size</span><span class="special">()));</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="identifier">capacity</span><span class="special">());</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="identifier">max_size</span><span class="special">());</span>
    <span class="special">}</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">allocator_type</span> <span class="identifier">allocator_type</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">pointer</span> <span class="identifier">pointer</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">const_pointer</span> <span class="identifier">const_pointer</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">reference</span> <span class="identifier">reference</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">const_reference</span> <span class="identifier">const_reference</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">value_type</span> <span class="identifier">value_type</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">iterator</span> <span class="identifier">iterator</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">const_iterator</span> <span class="identifier">const_iterator</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">size_type</span> <span class="identifier">size_type</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">difference_type</span> <span class="identifier">difference_type</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">reverse_iterator</span>
            <span class="identifier">reverse_iterator</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">const_reverse_iterator</span>
            <span class="identifier">const_reverse_iterator</span><span class="special">;</span>

    <span class="identifier">vector</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">vect_</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">empty</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">explicit</span> <span class="identifier">vector</span><span class="special">(</span><span class="identifier">Allocator</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">alloc</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">vect_</span><span class="special">(</span><span class="identifier">alloc</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">empty</span><span class="special">());</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">get_allocator</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">alloc</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">explicit</span> <span class="identifier">vector</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">count</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">vect_</span><span class="special">(</span><span class="identifier">count</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">count</span><span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span>
                    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">condition_if</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;(</span>
                        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">all_of_equal_to</span><span class="special">(),</span> <span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">end</span><span class="special">(),</span> <span class="identifier">T</span><span class="special">())</span>
                    <span class="special">)</span>
                <span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>
    <span class="special">}</span>

    <span class="identifier">vector</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">count</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">vect_</span><span class="special">(</span><span class="identifier">count</span><span class="special">,</span> <span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">count</span><span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span>
                    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">condition_if</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;(</span>
                        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">all_of_equal_to</span><span class="special">(),</span> <span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">end</span><span class="special">(),</span>
                                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(</span><span class="identifier">value</span><span class="special">))</span>
                    <span class="special">)</span>
                <span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>
    <span class="special">}</span>

    <span class="identifier">vector</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">count</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">,</span> <span class="identifier">Allocator</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">alloc</span><span class="special">)</span> <span class="special">:</span>
            <span class="identifier">vect_</span><span class="special">(</span><span class="identifier">count</span><span class="special">,</span> <span class="identifier">value</span><span class="special">,</span> <span class="identifier">alloc</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">count</span><span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span>
                    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">condition_if</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;(</span>
                        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">all_of_equal_to</span><span class="special">(),</span> <span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">end</span><span class="special">(),</span>
                                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(</span><span class="identifier">value</span><span class="special">))</span>
                    <span class="special">)</span>
                <span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">get_allocator</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">alloc</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">InputIter</span><span class="special">&gt;</span>
    <span class="identifier">vector</span><span class="special">(</span><span class="identifier">InputIter</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">InputIter</span> <span class="identifier">last</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">vect_</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">)</span> <span class="special">==</span>
                        <span class="keyword">int</span><span class="special">(</span><span class="identifier">size</span><span class="special">()));</span>
            <span class="special">})</span>
        <span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">InputIter</span><span class="special">&gt;</span>
    <span class="identifier">vector</span><span class="special">(</span><span class="identifier">InputIter</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">InputIter</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">Allocator</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">alloc</span><span class="special">)</span> <span class="special">:</span>
            <span class="identifier">vect_</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">alloc</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">)</span> <span class="special">==</span>
                        <span class="keyword">int</span><span class="special">(</span><span class="identifier">size</span><span class="special">()));</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">get_allocator</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">alloc</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">/* implicit */</span> <span class="identifier">vector</span><span class="special">(</span><span class="identifier">vector</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">vect_</span><span class="special">(</span><span class="identifier">other</span><span class="special">.</span><span class="identifier">vect_</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span>
                    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">condition_if</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;(</span>
                        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;(),</span>
                                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(*</span><span class="keyword">this</span><span class="special">),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(</span><span class="identifier">other</span><span class="special">))</span>
                    <span class="special">)</span>
                <span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>
    <span class="special">}</span>

    <span class="identifier">vector</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span><span class="identifier">vector</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&amp;&gt;</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span>
                    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">condition_if</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;(</span>
                        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;(),</span>
                                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(*</span><span class="keyword">this</span><span class="special">),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(</span><span class="identifier">other</span><span class="special">))</span>
                    <span class="special">)</span>
                <span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span>
                    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">condition_if</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;(</span>
                        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;(),</span>
                                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(*</span><span class="identifier">result</span><span class="special">),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(*</span><span class="keyword">this</span><span class="special">))</span>
                    <span class="special">)</span>
                <span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">if</span><span class="special">(</span><span class="keyword">this</span> <span class="special">!=</span> <span class="special">&amp;</span><span class="identifier">other</span><span class="special">)</span> <span class="identifier">vect_</span> <span class="special">=</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">vect_</span><span class="special">;</span>
        <span class="keyword">return</span> <span class="special">*(</span><span class="identifier">result</span> <span class="special">=</span> <span class="special">*</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">vector</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">void</span> <span class="identifier">reserve</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">count</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span> <span class="special">&lt;</span> <span class="identifier">max_size</span><span class="special">());</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">count</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">reserve</span><span class="special">(</span><span class="identifier">count</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="identifier">size_type</span> <span class="identifier">capacity</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">size_type</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">&gt;=</span> <span class="identifier">size</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">capacity</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="identifier">iterator</span> <span class="identifier">begin</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">iterator</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="keyword">if</span><span class="special">(</span><span class="identifier">empty</span><span class="special">())</span> <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="identifier">end</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="identifier">const_iterator</span> <span class="identifier">begin</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">const_iterator</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="keyword">if</span><span class="special">(</span><span class="identifier">empty</span><span class="special">())</span> <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="identifier">end</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="identifier">iterator</span> <span class="identifier">end</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">end</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="identifier">const_iterator</span> <span class="identifier">end</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">end</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="identifier">reverse_iterator</span> <span class="identifier">rbegin</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">iterator</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="keyword">if</span><span class="special">(</span><span class="identifier">empty</span><span class="special">())</span> <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="identifier">rend</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">rbegin</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="identifier">const_reverse_iterator</span> <span class="identifier">rbegin</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">const_reverse_iterator</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="keyword">if</span><span class="special">(</span><span class="identifier">empty</span><span class="special">())</span> <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="identifier">rend</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">rbegin</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="identifier">reverse_iterator</span> <span class="identifier">rend</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">rend</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="identifier">const_reverse_iterator</span> <span class="identifier">rend</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">rend</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="keyword">void</span> <span class="identifier">resize</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">count</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span> <span class="special">=</span> <span class="identifier">T</span><span class="special">())</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">size_type</span><span class="special">&gt;</span> <span class="identifier">old_size</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">size</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">count</span><span class="special">);</span>
                <span class="keyword">if</span><span class="special">(</span><span class="identifier">count</span> <span class="special">&gt;</span> <span class="special">*</span><span class="identifier">old_size</span><span class="special">)</span> <span class="special">{</span>
                    <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span>
                        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">condition_if</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;(</span>
                            <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">all_of_equal_to</span><span class="special">(),</span> <span class="identifier">begin</span><span class="special">()</span> <span class="special">+</span> <span class="special">*</span><span class="identifier">old_size</span><span class="special">,</span>
                                    <span class="identifier">end</span><span class="special">(),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(</span><span class="identifier">value</span><span class="special">))</span>
                        <span class="special">)</span>
                    <span class="special">);</span>
                <span class="special">}</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">resize</span><span class="special">(</span><span class="identifier">count</span><span class="special">,</span> <span class="identifier">value</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="identifier">size_type</span> <span class="identifier">size</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">size_type</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">&lt;=</span> <span class="identifier">capacity</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">size</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="identifier">size_type</span> <span class="identifier">max_size</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">size_type</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">&gt;=</span> <span class="identifier">capacity</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">max_size</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="keyword">bool</span> <span class="identifier">empty</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="keyword">bool</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">empty</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="identifier">Allocator</span> <span class="identifier">get_allocator</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">get_allocator</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="identifier">reference</span> <span class="identifier">at</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">index</span><span class="special">)</span> <span class="special">{</span>
        <span class="comment">// Check invariants, no pre (throw out_of_range for invalid index).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="identifier">index</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="identifier">const_reference</span> <span class="identifier">at</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">index</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants, no pre (throw out_of_range for invalid index).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="identifier">index</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="identifier">reference</span> <span class="keyword">operator</span><span class="special">[](</span><span class="identifier">size_type</span> <span class="identifier">index</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">index</span> <span class="special">&lt;</span> <span class="identifier">size</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">[</span><span class="identifier">index</span><span class="special">];</span>
    <span class="special">}</span>

    <span class="identifier">const_reference</span> <span class="keyword">operator</span><span class="special">[](</span><span class="identifier">size_type</span> <span class="identifier">index</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">index</span> <span class="special">&lt;</span> <span class="identifier">size</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">[</span><span class="identifier">index</span><span class="special">];</span>
    <span class="special">}</span>

    <span class="identifier">reference</span> <span class="identifier">front</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">empty</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">front</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="identifier">const_reference</span> <span class="identifier">front</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">empty</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">front</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="identifier">reference</span> <span class="identifier">back</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">empty</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">back</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="identifier">const_reference</span> <span class="identifier">back</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">empty</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">back</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="keyword">void</span> <span class="identifier">push_back</span><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">size_type</span><span class="special">&gt;</span> <span class="identifier">old_size</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">size</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">size_type</span><span class="special">&gt;</span> <span class="identifier">old_capacity</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">&lt;</span> <span class="identifier">max_size</span><span class="special">());</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_size</span> <span class="special">+</span> <span class="number">1</span><span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="special">*</span><span class="identifier">old_capacity</span><span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span>
                    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">condition_if</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;(</span>
                        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;(),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(</span><span class="identifier">back</span><span class="special">()),</span>
                                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(</span><span class="identifier">value</span><span class="special">))</span>
                    <span class="special">)</span>
                <span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">value</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">void</span> <span class="identifier">pop_back</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">size_type</span><span class="special">&gt;</span> <span class="identifier">old_size</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">size</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">empty</span><span class="special">());</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_size</span> <span class="special">-</span> <span class="number">1</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">pop_back</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">InputIter</span><span class="special">&gt;</span>
    <span class="keyword">void</span> <span class="identifier">assign</span><span class="special">(</span><span class="identifier">InputIter</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">InputIter</span> <span class="identifier">last</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT_AXIOM</span><span class="special">(</span>
                        <span class="special">!</span><span class="identifier">contained</span><span class="special">(</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">end</span><span class="special">(),</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">));</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">)</span> <span class="special">==</span>
                        <span class="keyword">int</span><span class="special">(</span><span class="identifier">size</span><span class="special">()));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">assign</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">void</span> <span class="identifier">assign</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">count</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span> <span class="special">&lt;=</span> <span class="identifier">max_size</span><span class="special">());</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span>
                    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">condition_if</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;(</span>
                        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">all_of_equal_to</span><span class="special">(),</span> <span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">end</span><span class="special">(),</span>
                                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(</span><span class="identifier">value</span><span class="special">))</span>
                    <span class="special">)</span>
                <span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">assign</span><span class="special">(</span><span class="identifier">count</span><span class="special">,</span> <span class="identifier">value</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="identifier">iterator</span> <span class="identifier">insert</span><span class="special">(</span><span class="identifier">iterator</span> <span class="identifier">where</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">iterator</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">size_type</span><span class="special">&gt;</span> <span class="identifier">old_size</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">size</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">size_type</span><span class="special">&gt;</span> <span class="identifier">old_capacity</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">&lt;</span> <span class="identifier">max_size</span><span class="special">());</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_size</span> <span class="special">+</span> <span class="number">1</span><span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="special">*</span><span class="identifier">old_capacity</span><span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span>
                    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">condition_if</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;(</span>
                        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;(),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(*</span><span class="identifier">result</span><span class="special">),</span>
                                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(</span><span class="identifier">value</span><span class="special">))</span>
                    <span class="special">)</span>
                <span class="special">);</span>
                <span class="keyword">if</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">&gt;</span> <span class="special">*</span><span class="identifier">old_capacity</span><span class="special">)</span> <span class="special">{</span>
                    <span class="identifier">BOOST_CONTRACT_ASSERT_AXIOM</span><span class="special">(!</span><span class="identifier">valid</span><span class="special">(</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">end</span><span class="special">()));</span>
                <span class="special">}</span> <span class="keyword">else</span> <span class="special">{</span>
                    <span class="identifier">BOOST_CONTRACT_ASSERT_AXIOM</span><span class="special">(!</span><span class="identifier">valid</span><span class="special">(</span><span class="identifier">where</span><span class="special">,</span> <span class="identifier">end</span><span class="special">()));</span>
                <span class="special">}</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">where</span><span class="special">,</span> <span class="identifier">value</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">void</span> <span class="identifier">insert</span><span class="special">(</span><span class="identifier">iterator</span> <span class="identifier">where</span><span class="special">,</span> <span class="identifier">size_type</span> <span class="identifier">count</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">size_type</span><span class="special">&gt;</span> <span class="identifier">old_size</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">size</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">size_type</span><span class="special">&gt;</span> <span class="identifier">old_capacity</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">iterator</span><span class="special">&gt;</span> <span class="identifier">old_where</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">where</span><span class="special">);</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">+</span> <span class="identifier">count</span> <span class="special">&lt;</span> <span class="identifier">max_size</span><span class="special">());</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_size</span> <span class="special">+</span> <span class="identifier">count</span><span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="special">*</span><span class="identifier">old_capacity</span><span class="special">);</span>
                <span class="keyword">if</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_capacity</span><span class="special">)</span> <span class="special">{</span>
                    <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span>
                        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">condition_if</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;(</span>
                            <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">all_of_equal_to</span><span class="special">(),</span>
                                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">prior</span><span class="special">(*</span><span class="identifier">old_where</span><span class="special">),</span>
                                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">prior</span><span class="special">(*</span><span class="identifier">old_where</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">count</span><span class="special">,</span>
                                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(</span><span class="identifier">value</span><span class="special">)</span>
                            <span class="special">)</span>
                        <span class="special">)</span>
                    <span class="special">);</span>
                    <span class="identifier">BOOST_CONTRACT_ASSERT_AXIOM</span><span class="special">(!</span><span class="identifier">valid</span><span class="special">(</span><span class="identifier">where</span><span class="special">,</span> <span class="identifier">end</span><span class="special">()));</span>
                <span class="special">}</span> <span class="keyword">else</span> <span class="identifier">BOOST_CONTRACT_ASSERT_AXIOM</span><span class="special">(!</span><span class="identifier">valid</span><span class="special">(</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">end</span><span class="special">()));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">where</span><span class="special">,</span> <span class="identifier">count</span><span class="special">,</span> <span class="identifier">value</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">InputIter</span><span class="special">&gt;</span>
    <span class="keyword">void</span> <span class="identifier">insert</span><span class="special">(</span><span class="identifier">iterator</span> <span class="identifier">where</span><span class="special">,</span> <span class="identifier">InputIter</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">InputIter</span> <span class="identifier">last</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">size_type</span><span class="special">&gt;</span> <span class="identifier">old_size</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">size</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">size_type</span><span class="special">&gt;</span> <span class="identifier">old_capacity</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">iterator</span><span class="special">&gt;</span> <span class="identifier">old_where</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">where</span><span class="special">);</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">+</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">)</span> <span class="special">&lt;</span>
                        <span class="identifier">max_size</span><span class="special">());</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT_AXIOM</span><span class="special">(</span>
                        <span class="special">!</span><span class="identifier">contained</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">end</span><span class="special">()));</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_size</span><span class="special">()</span> <span class="special">+</span>
                        <span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">));</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="special">*</span><span class="identifier">old_capacity</span><span class="special">);</span>
                <span class="keyword">if</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_capacity</span><span class="special">)</span> <span class="special">{</span>
                    <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span>
                        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">condition_if</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;(</span>
                            <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">all_of_equal_to</span><span class="special">(),</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">,</span>
                                    <span class="special">*</span><span class="identifier">old_where</span><span class="special">)</span>
                        <span class="special">)</span>
                    <span class="special">);</span>
                    <span class="identifier">BOOST_CONTRACT_ASSERT_AXIOM</span><span class="special">(!</span><span class="identifier">valid</span><span class="special">(</span><span class="identifier">where</span><span class="special">,</span> <span class="identifier">end</span><span class="special">()));</span>
                <span class="special">}</span> <span class="keyword">else</span> <span class="identifier">BOOST_CONTRACT_ASSERT_AXIOM</span><span class="special">(!</span><span class="identifier">valid</span><span class="special">(</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">end</span><span class="special">()));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">where</span><span class="special">,</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="identifier">iterator</span> <span class="identifier">erase</span><span class="special">(</span><span class="identifier">iterator</span> <span class="identifier">where</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">iterator</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">size_type</span><span class="special">&gt;</span> <span class="identifier">old_size</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">size</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">empty</span><span class="special">());</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">where</span> <span class="special">!=</span> <span class="identifier">end</span><span class="special">());</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_size</span> <span class="special">-</span> <span class="number">1</span><span class="special">);</span>
                <span class="keyword">if</span><span class="special">(</span><span class="identifier">empty</span><span class="special">())</span> <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="identifier">end</span><span class="special">());</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT_AXIOM</span><span class="special">(!</span><span class="identifier">valid</span><span class="special">(</span><span class="identifier">where</span><span class="special">,</span> <span class="identifier">end</span><span class="special">()));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">erase</span><span class="special">(</span><span class="identifier">where</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="identifier">iterator</span> <span class="identifier">erase</span><span class="special">(</span><span class="identifier">iterator</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">iterator</span> <span class="identifier">last</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">iterator</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">size_type</span><span class="special">&gt;</span> <span class="identifier">old_size</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">size</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">));</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_size</span> <span class="special">-</span>
                        <span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">));</span>
                <span class="keyword">if</span><span class="special">(</span><span class="identifier">empty</span><span class="special">())</span> <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="identifier">end</span><span class="special">());</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT_AXIOM</span><span class="special">(!</span><span class="identifier">valid</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">erase</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">void</span> <span class="identifier">clear</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">empty</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">clear</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span><span class="identifier">vector</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&gt;</span> <span class="identifier">old_me</span><span class="special">,</span> <span class="identifier">old_other</span><span class="special">;</span>
        <span class="preprocessor">#ifdef</span> <span class="identifier">BOOST_CONTRACT_AUDITS</span>
            <span class="identifier">old_me</span> <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(*</span><span class="keyword">this</span><span class="special">);</span>
            <span class="identifier">old_other</span> <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">other</span><span class="special">);</span>
        <span class="preprocessor">#endif</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">get_allocator</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">get_allocator</span><span class="special">());</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT_AUDIT</span><span class="special">(</span>
                    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">condition_if</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span>
                            <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="special">&gt;(</span>
                        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;(),</span>
                                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(*</span><span class="keyword">this</span><span class="special">),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(*</span><span class="identifier">old_other</span><span class="special">))</span>
                    <span class="special">)</span>
                <span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT_AUDIT</span><span class="special">(</span>
                    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">condition_if</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span>
                            <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="special">&gt;(</span>
                        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;(),</span>
                                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(</span><span class="identifier">other</span><span class="special">),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(*</span><span class="identifier">old_me</span><span class="special">))</span>
                    <span class="special">)</span>
                <span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">swap</span><span class="special">(</span><span class="identifier">other</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">friend</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">==(</span><span class="identifier">vector</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">left</span><span class="special">,</span> <span class="identifier">vector</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">right</span><span class="special">)</span> <span class="special">{</span>
        <span class="comment">// Check class invariants for left and right objects.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">left_inv</span> <span class="special">=</span>
                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(&amp;</span><span class="identifier">left</span><span class="special">);</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">right_inv</span> <span class="special">=</span>
                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(&amp;</span><span class="identifier">right</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">left</span><span class="special">.</span><span class="identifier">vect_</span> <span class="special">==</span> <span class="identifier">right</span><span class="special">.</span><span class="identifier">vect_</span><span class="special">;</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;</span> <span class="identifier">vect_</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="comment">// char type has operator==.</span>

    <span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">3</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">algorithm</span><span class="special">::</span><span class="identifier">all_of_equal</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="char">'\0'</span><span class="special">));</span>

    <span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">cv</span> <span class="special">=</span> <span class="identifier">v</span><span class="special">;</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">cv</span> <span class="special">==</span> <span class="identifier">v</span><span class="special">);</span>

    <span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;</span> <span class="identifier">w</span><span class="special">(</span><span class="identifier">v</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">w</span> <span class="special">==</span> <span class="identifier">v</span><span class="special">);</span>

    <span class="keyword">typename</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;::</span><span class="identifier">iterator</span> <span class="identifier">i</span> <span class="special">=</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
    <span class="identifier">assert</span><span class="special">(*</span><span class="identifier">i</span> <span class="special">==</span> <span class="char">'\0'</span><span class="special">);</span>

    <span class="keyword">typename</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;::</span><span class="identifier">const_iterator</span> <span class="identifier">ci</span> <span class="special">=</span> <span class="identifier">cv</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
    <span class="identifier">assert</span><span class="special">(*</span><span class="identifier">ci</span> <span class="special">==</span> <span class="char">'\0'</span><span class="special">);</span>

    <span class="identifier">v</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="char">'a'</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">v</span><span class="special">[</span><span class="number">0</span><span class="special">]</span> <span class="special">==</span> <span class="char">'a'</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">v</span><span class="special">[</span><span class="number">1</span><span class="special">]</span> <span class="special">==</span> <span class="char">'a'</span><span class="special">);</span>

    <span class="identifier">v</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="char">'b'</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">back</span><span class="special">()</span> <span class="special">==</span> <span class="char">'b'</span><span class="special">);</span>

    <span class="keyword">struct</span> <span class="identifier">x</span> <span class="special">{};</span> <span class="comment">// x type doest not have operator==.</span>

    <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">x</span><span class="special">&gt;</span> <span class="identifier">y</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">y</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">3</span><span class="special">);</span>

    <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">x</span><span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">cy</span> <span class="special">=</span> <span class="identifier">y</span><span class="special">;</span>
    <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">x</span><span class="special">&gt;</span> <span class="identifier">z</span><span class="special">(</span><span class="identifier">y</span><span class="special">);</span>

    <span class="keyword">typename</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">x</span><span class="special">&gt;::</span><span class="identifier">iterator</span> <span class="identifier">j</span> <span class="special">=</span> <span class="identifier">y</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">j</span> <span class="special">!=</span> <span class="identifier">y</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span>
    <span class="keyword">typename</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">x</span><span class="special">&gt;::</span><span class="identifier">const_iterator</span> <span class="identifier">cj</span> <span class="special">=</span> <span class="identifier">cy</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">cj</span> <span class="special">!=</span> <span class="identifier">cy</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span>

    <span class="identifier">y</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">j</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="identifier">x</span><span class="special">());</span>
    <span class="identifier">y</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">x</span><span class="special">());</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
                </p>
              </td>
<td>
                <p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="comment">// Extra spaces, newlines, etc. for visual alignment with this library code.</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">algorithm</span><span class="special">/</span><span class="identifier">cxx11</span><span class="special">/</span><span class="identifier">all_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_traits</span><span class="special">/</span><span class="identifier">has_equal_to</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">next_prior</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iterator</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">memory</span><span class="special">&gt;</span>





























<span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Allocator</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">vector</span> <span class="special">{</span>


    <span class="identifier">invariant</span> <span class="special">{</span>
        <span class="identifier">empty</span><span class="special">()</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">end</span><span class="special">())</span> <span class="special">==</span> <span class="keyword">int</span><span class="special">(</span><span class="identifier">size</span><span class="special">());</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">rbegin</span><span class="special">(),</span> <span class="identifier">rend</span><span class="special">())</span> <span class="special">==</span> <span class="keyword">int</span><span class="special">(</span><span class="identifier">size</span><span class="special">());</span>
        <span class="identifier">size</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="identifier">capacity</span><span class="special">();</span>
        <span class="identifier">capacity</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="identifier">max_size</span><span class="special">();</span>
    <span class="special">}</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">allocator_type</span> <span class="identifier">allocator_type</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">pointer</span> <span class="identifier">pointer</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">const_pointer</span> <span class="identifier">const_pointer</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">reference</span> <span class="identifier">reference</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">const_reference</span> <span class="identifier">const_reference</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">value_type</span> <span class="identifier">value_type</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">iterator</span> <span class="identifier">iterator</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">const_iterator</span> <span class="identifier">const_iterator</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">size_type</span> <span class="identifier">size_type</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">difference_type</span> <span class="identifier">difference_type</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">reverse_iterator</span>
            <span class="identifier">reverse_iterator</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">const_reverse_iterator</span>
            <span class="identifier">const_reverse_iterator</span><span class="special">;</span>

    <span class="identifier">vector</span><span class="special">()</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="identifier">empty</span><span class="special">();</span>
        <span class="special">}</span>
        <span class="special">:</span> <span class="identifier">vect_</span><span class="special">()</span>
    <span class="special">{}</span>


    <span class="keyword">explicit</span> <span class="identifier">vector</span><span class="special">(</span><span class="identifier">Allocator</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">alloc</span><span class="special">)</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="identifier">empty</span><span class="special">();</span>
            <span class="identifier">get_allocator</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">alloc</span><span class="special">;</span>
        <span class="special">}</span>
        <span class="special">:</span> <span class="identifier">vect_</span><span class="special">(</span><span class="identifier">alloc</span><span class="special">)</span>
    <span class="special">{}</span>


    <span class="keyword">explicit</span> <span class="identifier">vector</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">count</span><span class="special">)</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">count</span><span class="special">;</span>
            <span class="keyword">if</span> <span class="keyword">constexpr</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">algorithm</span><span class="special">::</span><span class="identifier">all_of_equal</span><span class="special">(</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">end</span><span class="special">(),</span> <span class="identifier">T</span><span class="special">());</span>
            <span class="special">}</span>
        <span class="special">}</span>
        <span class="special">:</span> <span class="identifier">vect_</span><span class="special">(</span><span class="identifier">count</span><span class="special">)</span>
    <span class="special">{}</span>




    <span class="identifier">vector</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">count</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">)</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">count</span><span class="special">;</span>
            <span class="keyword">if</span> <span class="keyword">constexpr</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">algorithm</span><span class="special">::</span><span class="identifier">all_of_equal</span><span class="special">(</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">end</span><span class="special">(),</span> <span class="identifier">value</span><span class="special">);</span>
            <span class="special">}</span>
        <span class="special">}</span>
        <span class="special">:</span> <span class="identifier">vect_</span><span class="special">(</span><span class="identifier">count</span><span class="special">,</span> <span class="identifier">value</span><span class="special">)</span>
    <span class="special">{}</span>





    <span class="identifier">vector</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">count</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">,</span> <span class="identifier">Allocator</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">alloc</span><span class="special">)</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">count</span><span class="special">;</span>
            <span class="keyword">if</span> <span class="keyword">constexpr</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">algorithm</span><span class="special">::</span><span class="identifier">all_of_equal</span><span class="special">(</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">end</span><span class="special">(),</span> <span class="identifier">value</span><span class="special">);</span>
            <span class="special">}</span>
            <span class="identifier">get_allocator</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">alloc</span><span class="special">;</span>
        <span class="special">}</span>
        <span class="special">:</span> <span class="identifier">vect_</span><span class="special">(</span><span class="identifier">count</span><span class="special">,</span> <span class="identifier">value</span><span class="special">,</span> <span class="identifier">alloc</span><span class="special">)</span>
    <span class="special">{}</span>






    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">InputIter</span><span class="special">&gt;</span>
    <span class="identifier">vector</span><span class="special">(</span><span class="identifier">InputIter</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">InputIter</span> <span class="identifier">last</span><span class="special">)</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">)</span> <span class="special">==</span> <span class="keyword">int</span><span class="special">(</span><span class="identifier">size</span><span class="special">());</span>
        <span class="special">}</span>
        <span class="special">:</span> <span class="identifier">vect_</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">)</span>
    <span class="special">{}</span>



    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">InputIter</span><span class="special">&gt;</span>
    <span class="identifier">vector</span><span class="special">(</span><span class="identifier">InputIter</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">InputIter</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">Allocator</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">alloc</span><span class="special">)</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">)</span> <span class="special">==</span> <span class="keyword">int</span><span class="special">(</span><span class="identifier">size</span><span class="special">());</span>
            <span class="identifier">get_allocator</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">alloc</span><span class="special">;</span>
        <span class="special">}</span>
        <span class="special">:</span> <span class="identifier">vect_</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">alloc</span><span class="special">)</span>
    <span class="special">{}</span>




    <span class="comment">/* implicit */</span> <span class="identifier">vector</span><span class="special">(</span><span class="identifier">vector</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="keyword">if</span> <span class="keyword">constexpr</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
                <span class="special">*</span><span class="keyword">this</span> <span class="special">==</span> <span class="identifier">other</span><span class="special">;</span>
            <span class="special">}</span>
        <span class="special">}</span>
        <span class="special">:</span> <span class="identifier">vect_</span><span class="special">(</span><span class="identifier">other</span><span class="special">.</span><span class="identifier">vect_</span><span class="special">)</span>
    <span class="special">{}</span>





    <span class="identifier">vector</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span><span class="identifier">vector</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span>
        <span class="identifier">postcondition</span><span class="special">(</span><span class="identifier">result</span><span class="special">)</span> <span class="special">{</span>
            <span class="keyword">if</span> <span class="keyword">constexpr</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
                <span class="special">*</span><span class="keyword">this</span> <span class="special">==</span> <span class="identifier">other</span><span class="special">;</span>
                <span class="identifier">result</span> <span class="special">==</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span>
            <span class="special">}</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">if</span><span class="special">(</span><span class="keyword">this</span> <span class="special">!=</span> <span class="special">&amp;</span><span class="identifier">other</span><span class="special">)</span> <span class="identifier">vect_</span> <span class="special">=</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">vect_</span><span class="special">;</span>
        <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span>
    <span class="special">}</span>












    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">vector</span><span class="special">()</span> <span class="special">{}</span>




    <span class="keyword">void</span> <span class="identifier">reserve</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">count</span><span class="special">)</span>
        <span class="identifier">precondition</span> <span class="special">{</span>
            <span class="identifier">count</span> <span class="special">&lt;</span> <span class="identifier">max_size</span><span class="special">();</span>
        <span class="special">}</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="identifier">capacity</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">count</span><span class="special">;</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">reserve</span><span class="special">(</span><span class="identifier">count</span><span class="special">);</span>
    <span class="special">}</span>



    <span class="identifier">size_type</span> <span class="identifier">capacity</span><span class="special">()</span> <span class="keyword">const</span>
        <span class="identifier">postcondition</span><span class="special">(</span><span class="identifier">result</span><span class="special">)</span> <span class="special">{</span>
            <span class="identifier">result</span> <span class="special">&gt;=</span> <span class="identifier">size</span><span class="special">();</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">capacity</span><span class="special">();</span>
    <span class="special">}</span>




    <span class="identifier">iterator</span> <span class="identifier">begin</span><span class="special">()</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="keyword">if</span><span class="special">(</span><span class="identifier">empty</span><span class="special">())</span> <span class="identifier">result</span> <span class="special">==</span> <span class="identifier">end</span><span class="special">();</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
    <span class="special">}</span>




    <span class="identifier">const_iterator</span> <span class="identifier">begin</span><span class="special">()</span> <span class="keyword">const</span>
        <span class="identifier">postcondition</span><span class="special">(</span><span class="identifier">result</span><span class="special">)</span> <span class="special">{</span>
            <span class="keyword">if</span><span class="special">(</span><span class="identifier">empty</span><span class="special">())</span> <span class="identifier">result</span> <span class="special">==</span> <span class="identifier">end</span><span class="special">();</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
    <span class="special">}</span>




    <span class="identifier">iterator</span> <span class="identifier">end</span><span class="special">()</span> <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">end</span><span class="special">();</span>
    <span class="special">}</span>



    <span class="identifier">const_iterator</span> <span class="identifier">end</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">end</span><span class="special">();</span>
    <span class="special">}</span>



    <span class="identifier">reverse_iterator</span> <span class="identifier">rbegin</span><span class="special">()</span>
        <span class="identifier">postcondition</span><span class="special">(</span><span class="identifier">result</span><span class="special">)</span> <span class="special">{</span>
            <span class="keyword">if</span><span class="special">(</span><span class="identifier">empty</span><span class="special">())</span> <span class="identifier">result</span> <span class="special">==</span> <span class="identifier">rend</span><span class="special">();</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">rbegin</span><span class="special">();</span>
    <span class="special">}</span>




    <span class="identifier">const_reverse_iterator</span> <span class="identifier">rbegin</span><span class="special">()</span> <span class="keyword">const</span>
        <span class="identifier">postcondition</span><span class="special">(</span><span class="identifier">result</span><span class="special">)</span> <span class="special">{</span>
            <span class="keyword">if</span><span class="special">(</span><span class="identifier">empty</span><span class="special">())</span> <span class="identifier">result</span> <span class="special">==</span> <span class="identifier">rend</span><span class="special">();</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">rbegin</span><span class="special">();</span>
    <span class="special">}</span>




    <span class="identifier">reverse_iterator</span> <span class="identifier">rend</span><span class="special">()</span> <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">rend</span><span class="special">();</span>
    <span class="special">}</span>



    <span class="identifier">const_reverse_iterator</span> <span class="identifier">rend</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">rend</span><span class="special">();</span>
    <span class="special">}</span>



    <span class="keyword">void</span> <span class="identifier">resize</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">count</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span> <span class="special">=</span> <span class="identifier">T</span><span class="special">())</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">count</span><span class="special">;</span>
            <span class="keyword">if</span> <span class="keyword">constexpr</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
                <span class="keyword">if</span><span class="special">(</span><span class="identifier">count</span> <span class="special">&gt;</span> <span class="identifier">oldof</span><span class="special">(</span><span class="identifier">size</span><span class="special">()))</span> <span class="special">{</span>
                    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">algorithm</span><span class="special">::</span><span class="identifier">all_of_equal</span><span class="special">(</span><span class="identifier">begin</span><span class="special">()</span> <span class="special">+</span> <span class="identifier">oldof</span><span class="special">(</span><span class="identifier">size</span><span class="special">()),</span>
                            <span class="identifier">end</span><span class="special">(),</span> <span class="identifier">value</span><span class="special">);</span>
                <span class="special">}</span>
            <span class="special">}</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">resize</span><span class="special">(</span><span class="identifier">count</span><span class="special">,</span> <span class="identifier">value</span><span class="special">);</span>
    <span class="special">}</span>







    <span class="identifier">size_type</span> <span class="identifier">size</span><span class="special">()</span> <span class="keyword">const</span>
        <span class="identifier">postcondition</span><span class="special">(</span><span class="identifier">result</span><span class="special">)</span> <span class="special">{</span>
            <span class="identifier">result</span> <span class="special">&lt;=</span> <span class="identifier">capacity</span><span class="special">();</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">size</span><span class="special">();</span>
    <span class="special">}</span>




    <span class="identifier">size_type</span> <span class="identifier">max_size</span><span class="special">()</span> <span class="keyword">const</span>
        <span class="identifier">postcondition</span><span class="special">(</span><span class="identifier">result</span><span class="special">)</span> <span class="special">{</span>
            <span class="identifier">result</span> <span class="special">&gt;=</span> <span class="identifier">capacity</span><span class="special">();</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">max_size</span><span class="special">();</span>
    <span class="special">}</span>




    <span class="keyword">bool</span> <span class="identifier">empty</span><span class="special">()</span> <span class="keyword">const</span>
        <span class="identifier">postcondition</span><span class="special">(</span><span class="identifier">result</span><span class="special">)</span> <span class="special">{</span>
            <span class="identifier">result</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">empty</span><span class="special">();</span>
    <span class="special">}</span>




    <span class="identifier">Alloctor</span> <span class="identifier">get_allocator</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">get_allocator</span><span class="special">();</span>
    <span class="special">}</span>



    <span class="identifier">reference</span> <span class="identifier">at</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">index</span><span class="special">)</span> <span class="special">{</span>
        <span class="comment">// No precondition (throw out_of_range for invalid index).</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="identifier">index</span><span class="special">);</span>
    <span class="special">}</span>


    <span class="identifier">const_reference</span> <span class="identifier">at</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">index</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// No precondition (throw out_of_range for invalid index).</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="identifier">index</span><span class="special">);</span>
    <span class="special">}</span>


    <span class="identifier">reference</span> <span class="keyword">operator</span><span class="special">[](</span><span class="identifier">size_type</span> <span class="identifier">index</span><span class="special">)</span>
        <span class="identifier">precondition</span> <span class="special">{</span>
            <span class="identifier">index</span> <span class="special">&lt;</span> <span class="identifier">size</span><span class="special">();</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">[</span><span class="identifier">index</span><span class="special">];</span>
    <span class="special">}</span>



    <span class="identifier">const_reference</span> <span class="keyword">operator</span><span class="special">[](</span><span class="identifier">size_type</span> <span class="identifier">index</span><span class="special">)</span> <span class="keyword">const</span>
        <span class="identifier">precondition</span> <span class="special">{</span>
            <span class="identifier">index</span> <span class="special">&lt;</span> <span class="identifier">size</span><span class="special">();</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">[</span><span class="identifier">index</span><span class="special">];</span>
    <span class="special">}</span>



    <span class="identifier">reference</span> <span class="identifier">front</span><span class="special">()</span>
        <span class="identifier">precondition</span> <span class="special">{</span>
            <span class="special">!</span><span class="identifier">empty</span><span class="special">();</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">front</span><span class="special">();</span>
    <span class="special">}</span>



    <span class="identifier">const_reference</span> <span class="identifier">front</span><span class="special">()</span> <span class="keyword">const</span>
        <span class="identifier">precondition</span> <span class="special">{</span>
            <span class="special">!</span><span class="identifier">empty</span><span class="special">();</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">front</span><span class="special">();</span>
    <span class="special">}</span>



    <span class="identifier">reference</span> <span class="identifier">back</span><span class="special">()</span>
        <span class="identifier">precondition</span> <span class="special">{</span>
            <span class="special">!</span><span class="identifier">empty</span><span class="special">();</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">back</span><span class="special">();</span>
    <span class="special">}</span>



    <span class="identifier">const_reference</span> <span class="identifier">back</span><span class="special">()</span> <span class="keyword">const</span>
        <span class="identifier">precondition</span> <span class="special">{</span>
            <span class="special">!</span><span class="identifier">empty</span><span class="special">();</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">back</span><span class="special">();</span>
    <span class="special">}</span>



    <span class="keyword">void</span> <span class="identifier">push_back</span><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">)</span>
        <span class="identifier">precondition</span> <span class="special">{</span>
            <span class="identifier">size</span><span class="special">()</span> <span class="special">&lt;</span> <span class="identifier">max_size</span><span class="special">();</span>
        <span class="special">}</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">oldof</span><span class="special">(</span><span class="identifier">size</span><span class="special">())</span> <span class="special">+</span> <span class="number">1</span><span class="special">;</span>
            <span class="identifier">capacity</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">oldof</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">())</span>
            <span class="keyword">if</span> <span class="keyword">constexpr</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
                <span class="identifier">back</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">value</span><span class="special">;</span>
            <span class="special">}</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">value</span><span class="special">);</span>
    <span class="special">}</span>










    <span class="keyword">void</span> <span class="identifier">pop_back</span><span class="special">()</span>
        <span class="identifier">precondition</span> <span class="special">{</span>
            <span class="special">!</span><span class="identifier">empty</span><span class="special">();</span>
        <span class="special">}</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">oldof</span><span class="special">(</span><span class="identifier">size</span><span class="special">())</span> <span class="special">-</span> <span class="number">1</span><span class="special">;</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">pop_back</span><span class="special">();</span>
    <span class="special">}</span>





    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">InputIter</span><span class="special">&gt;</span>
    <span class="keyword">void</span> <span class="identifier">assign</span><span class="special">(</span><span class="identifier">InputIter</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">InputIter</span> <span class="identifier">last</span><span class="special">)</span>
        <span class="comment">// Precondition: [begin(), end()) does not contain [first, last).</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">)</span> <span class="special">==</span> <span class="keyword">int</span><span class="special">(</span><span class="identifier">size</span><span class="special">());</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">assign</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">);</span>
    <span class="special">}</span>







    <span class="keyword">void</span> <span class="identifier">assign</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">count</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">vallue</span><span class="special">)</span>
        <span class="identifier">precondition</span> <span class="special">{</span>
            <span class="identifier">count</span> <span class="special">&lt;=</span> <span class="identifier">max_size</span><span class="special">();</span>
        <span class="special">}</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="keyword">if</span> <span class="keyword">constexpr</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">algorithm</span><span class="special">::</span><span class="identifier">all_of_equal</span><span class="special">(</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">end</span><span class="special">(),</span> <span class="identifier">value</span><span class="special">);</span>
            <span class="special">}</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">assign</span><span class="special">(</span><span class="identifier">count</span><span class="special">,</span> <span class="identifier">value</span><span class="special">);</span>
    <span class="special">}</span>






    <span class="identifier">iterator</span> <span class="identifier">insert</span><span class="special">(</span><span class="identifier">iterator</span> <span class="identifier">where</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">)</span>
        <span class="identifier">precondition</span> <span class="special">{</span>
            <span class="identifier">size</span><span class="special">()</span> <span class="special">&lt;</span> <span class="identifier">max_size</span><span class="special">();</span>
        <span class="special">}</span>
        <span class="identifier">postcondition</span><span class="special">(</span><span class="identifier">result</span><span class="special">)</span> <span class="special">{</span>
            <span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">oldof</span><span class="special">(</span><span class="identifier">size</span><span class="special">())</span> <span class="special">+</span> <span class="number">1</span><span class="special">;</span>
            <span class="identifier">capacity</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">oldof</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">());</span>
            <span class="keyword">if</span> <span class="keyword">constexpr</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
                <span class="special">*</span><span class="identifier">result</span> <span class="special">==</span> <span class="identifier">value</span><span class="special">;</span>
            <span class="special">}</span>
            <span class="comment">//  if(capacity() &gt; oldof(capacity()))</span>
            <span class="comment">//      [begin(), end()) is invalid</span>
            <span class="comment">//  else</span>
            <span class="comment">//      [where, end()) is invalid</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">where</span><span class="special">,</span> <span class="identifier">value</span><span class="special">);</span>
    <span class="special">}</span>












    <span class="keyword">void</span> <span class="identifier">insert</span><span class="special">(</span><span class="identifier">iterator</span> <span class="identifier">where</span><span class="special">,</span> <span class="identifier">size_type</span> <span class="identifier">count</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">)</span>
        <span class="identifier">precondition</span> <span class="special">{</span>
            <span class="identifier">size</span><span class="special">()</span> <span class="special">+</span> <span class="identifier">count</span> <span class="special">&lt;</span> <span class="identifier">max_size</span><span class="special">();</span>
        <span class="special">}</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">oldof</span><span class="special">(</span><span class="identifier">size</span><span class="special">())</span> <span class="special">+</span> <span class="identifier">count</span><span class="special">;</span>
            <span class="identifier">capacity</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">oldof</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">());</span>
            <span class="keyword">if</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">oldof</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()))</span> <span class="special">{</span>
                <span class="keyword">if</span> <span class="keyword">constexpr</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
                    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">algorithm</span><span class="special">::</span><span class="identifier">all_of_equal</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">prior</span><span class="special">(</span><span class="identifier">oldof</span><span class="special">(</span><span class="identifier">where</span><span class="special">)),</span>
                            <span class="identifier">boost</span><span class="special">::</span><span class="identifier">prior</span><span class="special">(</span><span class="identifier">oldof</span><span class="special">(</span><span class="identifier">where</span><span class="special">))</span> <span class="special">+</span> <span class="identifier">count</span><span class="special">,</span> <span class="identifier">value</span><span class="special">);</span>
                <span class="special">}</span>
                <span class="comment">// [where, end()) is invalid</span>
            <span class="special">}</span>
            <span class="comment">// else [begin(), end()) is invalid</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">where</span><span class="special">,</span> <span class="identifier">count</span><span class="special">,</span> <span class="identifier">value</span><span class="special">);</span>
    <span class="special">}</span>













    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">InputIter</span><span class="special">&gt;</span>
    <span class="keyword">void</span> <span class="identifier">insert</span><span class="special">(</span><span class="identifier">iterator</span> <span class="identifier">where</span><span class="special">,</span> <span class="identifier">Iterator</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">Iterator</span> <span class="identifier">last</span><span class="special">)</span>
        <span class="identifier">precondition</span> <span class="special">{</span>
            <span class="identifier">size</span><span class="special">()</span> <span class="special">+</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">)</span> <span class="special">&lt;</span> <span class="identifier">max_size</span><span class="special">();</span>
            <span class="comment">// [first, last) is not contained in [begin(), end())</span>
        <span class="special">}</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">oldof</span><span class="special">(</span><span class="identifier">size</span><span class="special">())</span> <span class="special">+</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">);</span>
            <span class="identifier">capacity</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">oldof</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">());</span>
            <span class="keyword">if</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">oldof</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()))</span> <span class="special">{</span>
                <span class="keyword">if</span> <span class="keyword">constexpr</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
                    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">algorithm</span><span class="special">::</span><span class="identifier">all_of_equal</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">oldof</span><span class="special">(</span><span class="identifier">where</span><span class="special">));</span>
                <span class="special">}</span>
                <span class="comment">// [where, end()) is invalid</span>
            <span class="special">}</span>
            <span class="comment">// else [begin(), end()) is invalid</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">where</span><span class="special">,</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">);</span>
    <span class="special">}</span>














    <span class="identifier">iterator</span> <span class="identifier">erase</span><span class="special">(</span><span class="identifier">iterator</span> <span class="identifier">where</span><span class="special">)</span>
        <span class="identifier">precondition</span> <span class="special">{</span>
            <span class="special">!</span><span class="identifier">empty</span><span class="special">();</span>
            <span class="identifier">where</span> <span class="special">!=</span> <span class="identifier">end</span><span class="special">();</span>
        <span class="special">}</span>
        <span class="identifier">postcondition</span><span class="special">(</span><span class="identifier">result</span><span class="special">)</span> <span class="special">{</span>
            <span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">oldod</span> <span class="identifier">size</span><span class="special">()</span> <span class="special">-</span> <span class="number">1</span><span class="special">;</span>
            <span class="keyword">if</span><span class="special">(</span><span class="identifier">empty</span><span class="special">())</span> <span class="identifier">result</span> <span class="special">==</span> <span class="identifier">end</span><span class="special">();</span>
            <span class="comment">// [where, end()) is invalid</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">erase</span><span class="special">(</span><span class="identifier">where</span><span class="special">);</span>
    <span class="special">}</span>






    <span class="identifier">iterator</span> <span class="identifier">erase</span><span class="special">(</span><span class="identifier">iterator</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">iterator</span> <span class="identifier">last</span><span class="special">)</span>
        <span class="identifier">precondition</span> <span class="special">{</span>
            <span class="identifier">size</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">lasst</span><span class="special">);</span>
        <span class="special">}</span>
        <span class="identifier">postcondition</span><span class="special">(</span><span class="identifier">result</span><span class="special">)</span> <span class="special">{</span>
            <span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">oldof</span><span class="special">(</span><span class="identifier">size</span><span class="special">())</span> <span class="special">-</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">);</span>
            <span class="keyword">if</span><span class="special">(</span><span class="identifier">empty</span><span class="special">())</span> <span class="identifier">result</span> <span class="special">==</span> <span class="identifier">end</span><span class="special">();</span>
            <span class="comment">// [first, last) is invalid</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">erase</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">);</span>
    <span class="special">}</span>







    <span class="keyword">void</span> <span class="identifier">clear</span><span class="special">()</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="identifier">empty</span><span class="special">();</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">clear</span><span class="special">();</span>
    <span class="special">}</span>



    <span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span><span class="identifier">vector</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span>
        <span class="identifier">precondition</span> <span class="special">{</span>
            <span class="identifier">get_allocator</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">get_allocator</span><span class="special">();</span>
        <span class="special">}</span>
        <span class="identifier">postcondition</span> <span class="special">{</span>
            <span class="keyword">if</span> <span class="keyword">constexpr</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">has_equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
                <span class="special">*</span><span class="keyword">this</span> <span class="special">==</span> <span class="identifier">oldof</span><span class="special">(</span><span class="identifier">other</span><span class="special">);</span>
                <span class="identifier">other</span> <span class="special">==</span> <span class="identifier">oldof</span><span class="special">(*</span><span class="keyword">this</span><span class="special">);</span>
            <span class="special">}</span>
        <span class="special">}</span>
    <span class="special">{</span>
        <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">swap</span><span class="special">(</span><span class="identifier">other</span><span class="special">);</span>
    <span class="special">}</span>


















    <span class="keyword">friend</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">==(</span><span class="identifier">vector</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">left</span><span class="special">,</span> <span class="identifier">vector</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">right</span><span class="special">)</span> <span class="special">{</span>
        <span class="comment">// Cannot check class invariants for left and right objects.</span>
        <span class="keyword">return</span> <span class="identifier">left</span><span class="special">.</span><span class="identifier">vect_</span> <span class="special">==</span> <span class="identifier">right</span><span class="special">.</span><span class="identifier">vect_</span><span class="special">;</span>
    <span class="special">}</span>





<span class="keyword">private</span><span class="special">:</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;</span> <span class="identifier">vect_</span><span class="special">;</span>
<span class="special">};</span>












































<span class="comment">// End.</span>
</pre>
<p>
                </p>
              </td>
</tr></tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__n1962___circle__subcontracting"></a><a class="link" href="examples.html#boost_contract.examples.__n1962___circle__subcontracting" title="[N1962] Circle: Subcontracting">[N1962]
      Circle: Subcontracting</a>
</h3></div></div></div>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="keyword">class</span> <span class="identifier">shape</span> <span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">shape</span><span class="special">()</span> <span class="special">{}</span>

    <span class="keyword">virtual</span> <span class="keyword">unsigned</span> <span class="identifier">compute_area</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">unsigned</span> <span class="identifier">shape</span><span class="special">::</span><span class="identifier">compute_area</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
    <span class="keyword">unsigned</span> <span class="identifier">result</span><span class="special">;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">result</span><span class="special">,</span> <span class="keyword">this</span><span class="special">)</span>
        <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">(</span><span class="keyword">int</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">result</span><span class="special">)</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">&gt;</span> <span class="number">0</span><span class="special">);</span>
        <span class="special">})</span>
    <span class="special">;</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="keyword">false</span><span class="special">);</span>
    <span class="keyword">return</span> <span class="identifier">result</span><span class="special">;</span>
<span class="special">}</span>

<span class="keyword">class</span> <span class="identifier">circle</span>
    <span class="preprocessor">#define</span> <span class="identifier">BASES</span> <span class="keyword">public</span> <span class="identifier">shape</span>
    <span class="special">:</span> <span class="identifier">BASES</span>
<span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">typedef</span> <span class="identifier">BOOST_CONTRACT_BASE_TYPES</span><span class="special">(</span><span class="identifier">BASES</span><span class="special">)</span> <span class="identifier">base_types</span><span class="special">;</span>
    <span class="preprocessor">#undef</span> <span class="identifier">BASES</span>

    <span class="identifier">BOOST_CONTRACT_OVERRIDE</span><span class="special">(</span><span class="identifier">compute_area</span><span class="special">);</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="keyword">static</span> <span class="keyword">int</span> <span class="keyword">const</span> <span class="identifier">pi</span> <span class="special">=</span> <span class="number">3</span><span class="special">;</span> <span class="comment">// Truncated to int from 3.14...</span>

    <span class="keyword">explicit</span> <span class="identifier">circle</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">a_radius</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">radius_</span><span class="special">(</span><span class="identifier">a_radius</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">radius</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">a_radius</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="keyword">unsigned</span> <span class="identifier">compute_area</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="keyword">const</span>
            <span class="comment">/* override */</span> <span class="special">{</span>
        <span class="keyword">unsigned</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">&lt;</span>
                <span class="identifier">override_compute_area</span><span class="special">&gt;(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">result</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">circle</span><span class="special">::</span><span class="identifier">compute_area</span><span class="special">,</span> <span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">(</span><span class="keyword">unsigned</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">result</span><span class="special">)</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="identifier">pi</span> <span class="special">*</span> <span class="identifier">radius</span><span class="special">()</span> <span class="special">*</span> <span class="identifier">radius</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">pi</span> <span class="special">*</span> <span class="identifier">radius</span><span class="special">()</span> <span class="special">*</span> <span class="identifier">radius</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="keyword">unsigned</span> <span class="identifier">radius</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">radius_</span><span class="special">;</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="keyword">unsigned</span> <span class="identifier">radius_</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">circle</span> <span class="identifier">c</span><span class="special">(</span><span class="number">2</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">c</span><span class="special">.</span><span class="identifier">radius</span><span class="special">()</span> <span class="special">==</span> <span class="number">2</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">c</span><span class="special">.</span><span class="identifier">compute_area</span><span class="special">()</span> <span class="special">==</span> <span class="number">12</span><span class="special">);</span>
    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__n1962___factorial__recursion"></a><a name="N1962_factorial_anchor"></a><a class="link" href="examples.html#boost_contract.examples.__n1962___factorial__recursion" title="[N1962] Factorial: Recursion">[N1962]
      Factorial: Recursion</a>
</h3></div></div></div>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="keyword">int</span> <span class="identifier">factorial</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">n</span><span class="special">)</span> <span class="special">{</span>
    <span class="keyword">int</span> <span class="identifier">result</span><span class="special">;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">function</span><span class="special">()</span>
        <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">n</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span> <span class="comment">// Non-negative natural number.</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">n</span> <span class="special">&lt;=</span> <span class="number">12</span><span class="special">);</span> <span class="comment">// Max function input.</span>
        <span class="special">})</span>
        <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">&gt;=</span> <span class="number">1</span><span class="special">);</span>
            <span class="keyword">if</span><span class="special">(</span><span class="identifier">n</span> <span class="special">&lt;</span> <span class="number">2</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// Select assertion.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="number">1</span><span class="special">);</span>
            <span class="special">}</span> <span class="keyword">else</span> <span class="special">{</span>
                <span class="comment">// Assertions automatically disabled in other assertions.</span>
                <span class="comment">// Therefore, this postcondition can recursively call the</span>
                <span class="comment">// function without causing infinite recursion.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT_AUDIT</span><span class="special">(</span><span class="identifier">n</span> <span class="special">*</span> <span class="identifier">factorial</span><span class="special">(</span><span class="identifier">n</span> <span class="special">-</span> <span class="number">1</span><span class="special">));</span>
            <span class="special">}</span>
        <span class="special">})</span>
    <span class="special">;</span>

    <span class="keyword">return</span> <span class="identifier">n</span> <span class="special">&lt;</span> <span class="number">2</span> <span class="special">?</span> <span class="special">(</span><span class="identifier">result</span> <span class="special">=</span> <span class="number">1</span><span class="special">)</span> <span class="special">:</span> <span class="special">(</span><span class="identifier">result</span> <span class="special">=</span> <span class="identifier">n</span> <span class="special">*</span> <span class="identifier">factorial</span><span class="special">(</span><span class="identifier">n</span> <span class="special">-</span> <span class="number">1</span><span class="special">));</span>
<span class="special">}</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">factorial</span><span class="special">(</span><span class="number">4</span><span class="special">)</span> <span class="special">==</span> <span class="number">24</span><span class="special">);</span>
    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__n1962___equal__operators"></a><a class="link" href="examples.html#boost_contract.examples.__n1962___equal__operators" title="[N1962] Equal: Operators">[N1962]
      Equal: Operators</a>
</h3></div></div></div>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="comment">// Forward declaration because == and != contracts use one another's function.</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">==(</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">left</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">right</span><span class="special">);</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">!=(</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">left</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">right</span><span class="special">)</span> <span class="special">{</span>
    <span class="keyword">bool</span> <span class="identifier">result</span><span class="special">;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">function</span><span class="special">()</span>
        <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="special">!(</span><span class="identifier">left</span> <span class="special">==</span> <span class="identifier">right</span><span class="special">));</span>
        <span class="special">})</span>
    <span class="special">;</span>

    <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">left</span><span class="special">.</span><span class="identifier">value</span> <span class="special">!=</span> <span class="identifier">right</span><span class="special">.</span><span class="identifier">value</span><span class="special">);</span>
<span class="special">}</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">==(</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">left</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">right</span><span class="special">)</span> <span class="special">{</span>
    <span class="keyword">bool</span> <span class="identifier">result</span><span class="special">;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">function</span><span class="special">()</span>
        <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="special">!(</span><span class="identifier">left</span> <span class="special">!=</span> <span class="identifier">right</span><span class="special">));</span>
        <span class="special">})</span>
    <span class="special">;</span>

    <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">left</span><span class="special">.</span><span class="identifier">value</span> <span class="special">==</span> <span class="identifier">right</span><span class="special">.</span><span class="identifier">value</span><span class="special">);</span>
<span class="special">}</span>

<span class="keyword">struct</span> <span class="identifier">number</span> <span class="special">{</span> <span class="keyword">int</span> <span class="identifier">value</span><span class="special">;</span> <span class="special">};</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">number</span> <span class="identifier">n</span><span class="special">;</span>
    <span class="identifier">n</span><span class="special">.</span><span class="identifier">value</span> <span class="special">=</span> <span class="number">123</span><span class="special">;</span>

    <span class="identifier">assert</span><span class="special">((</span><span class="identifier">n</span> <span class="special">==</span> <span class="identifier">n</span><span class="special">)</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">);</span>   <span class="comment">// Explicitly call operator==.</span>
    <span class="identifier">assert</span><span class="special">((</span><span class="identifier">n</span> <span class="special">!=</span> <span class="identifier">n</span><span class="special">)</span> <span class="special">==</span> <span class="keyword">false</span><span class="special">);</span>  <span class="comment">// Explicitly call operator!=.</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__n1962___sum__array_parameter"></a><a class="link" href="examples.html#boost_contract.examples.__n1962___sum__array_parameter" title="[N1962] Sum: Array parameter">[N1962]
      Sum: Array parameter</a>
</h3></div></div></div>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="keyword">int</span> <span class="identifier">sum</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">count</span><span class="special">,</span> <span class="keyword">int</span><span class="special">*</span> <span class="identifier">array</span><span class="special">)</span> <span class="special">{</span>
    <span class="keyword">int</span> <span class="identifier">result</span><span class="special">;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">function</span><span class="special">()</span>
        <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span> <span class="special">%</span> <span class="number">4</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
        <span class="special">})</span>
    <span class="special">;</span>

    <span class="identifier">result</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
    <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">count</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="identifier">result</span> <span class="special">+=</span> <span class="identifier">array</span><span class="special">[</span><span class="identifier">i</span><span class="special">];</span>
    <span class="keyword">return</span> <span class="identifier">result</span><span class="special">;</span>
<span class="special">}</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="keyword">int</span> <span class="identifier">a</span><span class="special">[</span><span class="number">4</span><span class="special">]</span> <span class="special">=</span> <span class="special">{</span><span class="number">1</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="number">3</span><span class="special">,</span> <span class="number">4</span><span class="special">};</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">sum</span><span class="special">(</span><span class="number">4</span><span class="special">,</span> <span class="identifier">a</span><span class="special">)</span> <span class="special">==</span> <span class="number">10</span><span class="special">);</span>
    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__n1962___square_root__default_parameters_and_comparison_with_d_syntax"></a><a name="N1962_square_root_anchor"></a><a class="link" href="examples.html#boost_contract.examples.__n1962___square_root__default_parameters_and_comparison_with_d_syntax" title="[N1962] Square Root: Default parameters and comparison with D syntax">[N1962]
      Square Root: Default parameters and comparison with D syntax</a>
</h3></div></div></div>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  This Library
                </p>
              </th>
<th>
                <p>
                  The D Programming Language
                </p>
              </th>
</tr></thead>
<tbody><tr>
<td>
                <p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cmath</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="keyword">long</span> <span class="identifier">lsqrt</span><span class="special">(</span><span class="keyword">long</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span>
    <span class="keyword">long</span> <span class="identifier">result</span><span class="special">;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">function</span><span class="special">()</span>
        <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">x</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span>
        <span class="special">})</span>
        <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">*</span> <span class="identifier">result</span> <span class="special">&lt;=</span> <span class="identifier">x</span><span class="special">);</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">((</span><span class="identifier">result</span> <span class="special">+</span> <span class="number">1</span><span class="special">)</span> <span class="special">*</span> <span class="special">(</span><span class="identifier">result</span> <span class="special">+</span> <span class="number">1</span><span class="special">)</span> <span class="special">&gt;</span> <span class="identifier">x</span><span class="special">);</span>
        <span class="special">})</span>
    <span class="special">;</span>

    <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="keyword">long</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">sqrt</span><span class="special">(</span><span class="keyword">double</span><span class="special">(</span><span class="identifier">x</span><span class="special">)));</span>
<span class="special">}</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">lsqrt</span><span class="special">(</span><span class="number">4</span><span class="special">)</span> <span class="special">==</span> <span class="number">2</span><span class="special">);</span>
    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
                </p>
              </td>
<td>
                <p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="comment">// Extra spaces, newlines, etc. for visual alignment with this library code.</span>



<span class="keyword">long</span> <span class="identifier">lsqrt</span><span class="special">(</span><span class="keyword">long</span> <span class="identifier">x</span><span class="special">)</span>
<span class="identifier">in</span> <span class="special">{</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">x</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span>
<span class="special">}</span>
<span class="identifier">out</span><span class="special">(</span><span class="identifier">result</span><span class="special">)</span> <span class="special">{</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">result</span> <span class="special">*</span> <span class="identifier">result</span> <span class="special">&lt;=</span> <span class="identifier">x</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">((</span><span class="identifier">result</span> <span class="special">+</span> <span class="number">1</span><span class="special">)</span> <span class="special">*</span> <span class="special">(</span><span class="identifier">result</span> <span class="special">+</span> <span class="number">1</span><span class="special">)</span> <span class="special">&gt;</span> <span class="identifier">x</span><span class="special">);</span>
<span class="special">}</span>
<span class="keyword">do</span> <span class="special">{</span>
    <span class="keyword">return</span> <span class="identifier">cast</span><span class="special">(</span><span class="keyword">long</span><span class="special">)</span><span class="identifier">std</span><span class="special">.</span><span class="identifier">math</span><span class="special">.</span><span class="identifier">sqrt</span><span class="special">(</span><span class="identifier">cast</span><span class="special">(</span><span class="identifier">real</span><span class="special">)</span><span class="identifier">x</span><span class="special">);</span>
<span class="special">}</span>








<span class="comment">// End.</span>
</pre>
<p>
                </p>
              </td>
</tr></tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__meyer97___stack4__comparison_with_eiffel_syntax"></a><a name="Meyer97_stack4_anchor"></a><a class="link" href="examples.html#boost_contract.examples.__meyer97___stack4__comparison_with_eiffel_syntax" title="[Meyer97] Stack4: Comparison with Eiffel syntax">[Meyer97]
      Stack4: Comparison with Eiffel syntax</a>
</h3></div></div></div>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  This Library
                </p>
              </th>
<th>
                <p>
                  The Eiffel Programming Language
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
                <p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="comment">// File: stack4.hpp</span>
<span class="preprocessor">#ifndef</span> <span class="identifier">STACK4_HPP_</span>
<span class="preprocessor">#define</span> <span class="identifier">STACK4_HPP_</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="comment">// Dispenser with LIFO access policy and fixed max capacity.</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">stack4</span>
    <span class="preprocessor">#define</span> <span class="identifier">BASES</span> <span class="keyword">private</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor_precondition</span><span class="special">&lt;</span><span class="identifier">stack4</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;</span>
    <span class="special">:</span> <span class="identifier">BASES</span>
<span class="special">{</span>
    <span class="keyword">friend</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="identifier">BOOST_CONTRACT_BASE_TYPES</span><span class="special">(</span><span class="identifier">BASES</span><span class="special">)</span> <span class="identifier">base_types</span><span class="special">;</span>
    <span class="preprocessor">#undef</span> <span class="identifier">BASES</span>

    <span class="keyword">void</span> <span class="identifier">invariant</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span> <span class="comment">// Count non-negative.</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="identifier">capacity</span><span class="special">());</span> <span class="comment">// Count bounded.</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">empty</span><span class="special">()</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">));</span> <span class="comment">// Empty if no elem.</span>
    <span class="special">}</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="comment">/* Initialization */</span>

    <span class="comment">// Allocate static from a maximum of n elements.</span>
    <span class="keyword">explicit</span> <span class="identifier">stack4</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">n</span><span class="special">)</span> <span class="special">:</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor_precondition</span><span class="special">&lt;</span><span class="identifier">stack4</span><span class="special">&gt;([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">n</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span> <span class="comment">// Non-negative capacity.</span>
        <span class="special">})</span>
    <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">n</span><span class="special">);</span> <span class="comment">// Capacity set.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">capacity_</span> <span class="special">=</span> <span class="identifier">n</span><span class="special">;</span>
        <span class="identifier">count_</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
        <span class="identifier">array_</span> <span class="special">=</span> <span class="keyword">new</span> <span class="identifier">T</span><span class="special">[</span><span class="identifier">n</span><span class="special">];</span>
    <span class="special">}</span>

    <span class="comment">// Deep copy via constructor.</span>
    <span class="comment">/* implicit */</span> <span class="identifier">stack4</span><span class="special">(</span><span class="identifier">stack4</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">capacity</span><span class="special">());</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">count</span><span class="special">());</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT_AXIOM</span><span class="special">(*</span><span class="keyword">this</span> <span class="special">==</span> <span class="identifier">other</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">capacity_</span> <span class="special">=</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">capacity_</span><span class="special">;</span>
        <span class="identifier">count_</span> <span class="special">=</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">count_</span><span class="special">;</span>
        <span class="identifier">array_</span> <span class="special">=</span> <span class="keyword">new</span> <span class="identifier">T</span><span class="special">[</span><span class="identifier">other</span><span class="special">.</span><span class="identifier">capacity_</span><span class="special">];</span>
        <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">count_</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="identifier">array_</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">array_</span><span class="special">[</span><span class="identifier">i</span><span class="special">];</span>
    <span class="special">}</span>

    <span class="comment">// Deep copy via assignment.</span>
    <span class="identifier">stack4</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span><span class="identifier">stack4</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">capacity</span><span class="special">());</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">count</span><span class="special">());</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT_AXIOM</span><span class="special">(*</span><span class="keyword">this</span> <span class="special">==</span> <span class="identifier">other</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">delete</span><span class="special">[]</span> <span class="identifier">array_</span><span class="special">;</span>
        <span class="identifier">capacity_</span> <span class="special">=</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">capacity_</span><span class="special">;</span>
        <span class="identifier">count_</span> <span class="special">=</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">count_</span><span class="special">;</span>
        <span class="identifier">array_</span> <span class="special">=</span> <span class="keyword">new</span> <span class="identifier">T</span><span class="special">[</span><span class="identifier">other</span><span class="special">.</span><span class="identifier">capacity_</span><span class="special">];</span>
        <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">count_</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="identifier">array_</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">array_</span><span class="special">[</span><span class="identifier">i</span><span class="special">];</span>
        <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">// Destroy this stack.</span>
    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">stack4</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">delete</span><span class="special">[]</span> <span class="identifier">array_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">/* Access */</span>

    <span class="comment">// Max number of stack elements.</span>
    <span class="keyword">int</span> <span class="identifier">capacity</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">capacity_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">// Number of stack elements.</span>
    <span class="keyword">int</span> <span class="identifier">count</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">count_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">// Top element.</span>
    <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">item</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">empty</span><span class="special">());</span> <span class="comment">// Not empty (i.e., count &gt; 0).</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">array_</span><span class="special">[</span><span class="identifier">count_</span> <span class="special">-</span> <span class="number">1</span><span class="special">];</span>
    <span class="special">}</span>

    <span class="comment">/* Status Report */</span>

    <span class="comment">// Is stack empty?</span>
    <span class="keyword">bool</span> <span class="identifier">empty</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="keyword">bool</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Empty definition.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">count_</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">// Is stack full?</span>
    <span class="keyword">bool</span> <span class="identifier">full</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="keyword">bool</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span> <span class="comment">// Full definition.</span>
                        <span class="identifier">result</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">capacity</span><span class="special">()));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">count_</span> <span class="special">==</span> <span class="identifier">capacity_</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">/* Element Change */</span>

    <span class="comment">// Add x on top.</span>
    <span class="keyword">void</span> <span class="identifier">put</span><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">old_count</span> <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">count</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">full</span><span class="special">());</span> <span class="comment">// Not full.</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">empty</span><span class="special">());</span> <span class="comment">// Not empty.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">item</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">x</span><span class="special">);</span> <span class="comment">// Added to top.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_count</span> <span class="special">+</span> <span class="number">1</span><span class="special">);</span> <span class="comment">// One more.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">array_</span><span class="special">[</span><span class="identifier">count_</span><span class="special">++]</span> <span class="special">=</span> <span class="identifier">x</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">// Remove top element.</span>
    <span class="keyword">void</span> <span class="identifier">remove</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">old_count</span> <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">count</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">empty</span><span class="special">());</span> <span class="comment">// Not empty (i.e., count &gt; 0).</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">full</span><span class="special">());</span> <span class="comment">// Not full.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_count</span> <span class="special">-</span> <span class="number">1</span><span class="special">);</span> <span class="comment">// One less.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="special">--</span><span class="identifier">count_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">/* Friend Helpers */</span>

    <span class="keyword">friend</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">==(</span><span class="identifier">stack4</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">left</span><span class="special">,</span> <span class="identifier">stack4</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">right</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">inv1</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(&amp;</span><span class="identifier">left</span><span class="special">);</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">inv2</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(&amp;</span><span class="identifier">right</span><span class="special">);</span>
        <span class="keyword">if</span><span class="special">(</span><span class="identifier">left</span><span class="special">.</span><span class="identifier">count_</span> <span class="special">!=</span> <span class="identifier">right</span><span class="special">.</span><span class="identifier">count_</span><span class="special">)</span> <span class="keyword">return</span> <span class="keyword">false</span><span class="special">;</span>
        <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">left</span><span class="special">.</span><span class="identifier">count_</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
            <span class="keyword">if</span><span class="special">(</span><span class="identifier">left</span><span class="special">.</span><span class="identifier">array_</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> <span class="special">!=</span> <span class="identifier">right</span><span class="special">.</span><span class="identifier">array_</span><span class="special">[</span><span class="identifier">i</span><span class="special">])</span> <span class="keyword">return</span> <span class="keyword">false</span><span class="special">;</span>
        <span class="special">}</span>
        <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="keyword">int</span> <span class="identifier">capacity_</span><span class="special">;</span>
    <span class="keyword">int</span> <span class="identifier">count_</span><span class="special">;</span>
    <span class="identifier">T</span><span class="special">*</span> <span class="identifier">array_</span><span class="special">;</span> <span class="comment">// Internally use C-style array.</span>
<span class="special">};</span>

<span class="preprocessor">#endif</span> <span class="comment">// #include guard</span>
</pre>
<p>
                </p>
              </td>
<td>
                <p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">--</span> <span class="identifier">Extra</span> <span class="identifier">spaces</span><span class="special">,</span> <span class="identifier">newlines</span><span class="special">,</span> <span class="identifier">etc</span><span class="special">.</span> <span class="keyword">for</span> <span class="identifier">visual</span> <span class="identifier">alignment</span> <span class="identifier">with</span> <span class="keyword">this</span> <span class="identifier">library</span> <span class="identifier">code</span><span class="special">.</span>





<span class="identifier">indexing</span>
    <span class="identifier">destription</span><span class="special">:</span> <span class="string">"Dispenser with LIFO access policy and a fixed max capacity."</span>
<span class="keyword">class</span> <span class="identifier">interface</span> <span class="identifier">STACK4</span><span class="special">[</span><span class="identifier">G</span><span class="special">]</span> <span class="identifier">creation</span> <span class="identifier">make</span> <span class="special">--</span> <span class="identifier">Interface</span> <span class="identifier">only</span> <span class="special">(</span><span class="identifier">no</span> <span class="identifier">implementation</span><span class="special">).</span>







<span class="identifier">invariant</span>
    <span class="identifier">count_non_negative</span><span class="special">:</span> <span class="identifier">count</span> <span class="special">&gt;=</span> <span class="number">0</span>
    <span class="identifier">count_bounded</span><span class="special">:</span> <span class="identifier">count</span> <span class="special">&lt;=</span> <span class="identifier">capacity</span>
    <span class="identifier">empty_if_no_elements</span><span class="special">:</span> <span class="identifier">empty</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">count</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span>



<span class="identifier">feature</span> <span class="special">--</span> <span class="identifier">Initialization</span>

    <span class="special">--</span> <span class="identifier">Allocate</span> <span class="identifier">stack</span> <span class="keyword">for</span> <span class="identifier">a</span> <span class="identifier">maximum</span> <span class="identifier">of</span> <span class="identifier">n</span> <span class="identifier">elements</span><span class="special">.</span>
    <span class="identifier">make</span><span class="special">(</span><span class="identifier">n</span><span class="special">:</span> <span class="identifier">INTEGER</span><span class="special">)</span> <span class="identifier">is</span>
        <span class="identifier">require</span>
            <span class="identifier">non_negative_capacity</span><span class="special">:</span> <span class="identifier">n</span> <span class="special">&gt;=</span> <span class="number">0</span>
        <span class="identifier">ensure</span>
            <span class="identifier">capacity_set</span><span class="special">:</span> <span class="identifier">capacity</span> <span class="special">=</span> <span class="identifier">n</span>
        <span class="identifier">end</span>



















































<span class="identifier">feature</span> <span class="special">--</span> <span class="identifier">Access</span>

    <span class="special">--</span> <span class="identifier">Max</span> <span class="identifier">number</span> <span class="identifier">of</span> <span class="identifier">stack</span> <span class="identifier">elements</span><span class="special">.</span>
    <span class="identifier">capacity</span><span class="special">:</span> <span class="identifier">INTEGER</span>





    <span class="special">--</span> <span class="identifier">Number</span> <span class="identifier">of</span> <span class="identifier">stack</span> <span class="identifier">elements</span><span class="special">.</span>
    <span class="identifier">count</span><span class="special">:</span> <span class="identifier">INTEGER</span>





    <span class="special">--</span> <span class="identifier">Top</span> <span class="identifier">element</span><span class="special">.</span>
    <span class="identifier">item</span><span class="special">:</span> <span class="identifier">G</span> <span class="identifier">is</span>
        <span class="identifier">require</span>
            <span class="identifier">not_empty</span><span class="special">:</span> <span class="keyword">not</span> <span class="identifier">empty</span> <span class="special">--</span> <span class="identifier">i</span><span class="special">.</span><span class="identifier">e</span><span class="special">.,</span> <span class="identifier">count</span> <span class="special">&gt;</span> <span class="number">0</span>
        <span class="identifier">end</span>






<span class="identifier">feature</span> <span class="special">--</span> <span class="identifier">Status</span> <span class="identifier">report</span>

    <span class="special">--</span> <span class="identifier">Is</span> <span class="identifier">stack</span> <span class="identifier">empty</span><span class="special">?</span>
    <span class="identifier">empty</span><span class="special">:</span> <span class="identifier">BOOLEAN</span> <span class="identifier">is</span>
        <span class="identifier">ensure</span>
            <span class="identifier">empty_definition</span><span class="special">:</span> <span class="identifier">result</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">count</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span>
        <span class="identifier">end</span>








    <span class="special">--</span> <span class="identifier">Is</span> <span class="identifier">stack</span> <span class="identifier">full</span><span class="special">?</span>
    <span class="identifier">full</span><span class="special">:</span> <span class="identifier">BOOLEAN</span> <span class="identifier">is</span>
        <span class="identifier">ensure</span>
            <span class="identifier">full_definition</span><span class="special">:</span> <span class="identifier">result</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">count</span> <span class="special">=</span> <span class="identifier">capacity</span><span class="special">)</span>
        <span class="identifier">end</span>








<span class="identifier">feature</span> <span class="special">--</span> <span class="identifier">Element</span> <span class="identifier">change</span>

    <span class="special">--</span> <span class="identifier">Add</span> <span class="identifier">x</span> <span class="identifier">on</span> <span class="identifier">top</span><span class="special">.</span>
    <span class="identifier">put</span><span class="special">(</span><span class="identifier">x</span><span class="special">:</span> <span class="identifier">G</span><span class="special">)</span> <span class="identifier">is</span>
        <span class="identifier">require</span>
            <span class="identifier">not_full</span><span class="special">:</span> <span class="keyword">not</span> <span class="identifier">full</span>
        <span class="identifier">ensure</span>
            <span class="identifier">not_empty</span><span class="special">:</span> <span class="keyword">not</span> <span class="identifier">empty</span>
            <span class="identifier">added_to_top</span><span class="special">:</span> <span class="identifier">item</span> <span class="special">=</span> <span class="identifier">x</span>
            <span class="identifier">one_more_item</span><span class="special">:</span> <span class="identifier">count</span> <span class="special">=</span> <span class="identifier">old</span> <span class="identifier">count</span> <span class="special">+</span> <span class="number">1</span>
        <span class="identifier">end</span>








    <span class="special">--</span> <span class="identifier">Remove</span> <span class="identifier">top</span> <span class="identifier">element</span><span class="special">.</span>
    <span class="identifier">remove</span> <span class="identifier">is</span>
        <span class="identifier">require</span>
            <span class="identifier">not_empty</span><span class="special">:</span> <span class="keyword">not</span> <span class="identifier">empty</span> <span class="special">--</span> <span class="identifier">i</span><span class="special">.</span><span class="identifier">e</span><span class="special">.,</span> <span class="identifier">count</span> <span class="special">&gt;</span> <span class="number">0</span>
        <span class="identifier">ensure</span>
            <span class="identifier">not_full</span><span class="special">:</span> <span class="keyword">not</span> <span class="identifier">full</span>
            <span class="identifier">one_fewer_item</span><span class="special">:</span> <span class="identifier">count</span> <span class="special">=</span> <span class="identifier">old</span> <span class="identifier">count</span> <span class="special">-</span> <span class="number">1</span>

        <span class="identifier">end</span>























<span class="identifier">end</span> <span class="special">--</span> <span class="keyword">class</span> <span class="identifier">interface</span> <span class="identifier">STACK4</span>

<span class="special">--</span> <span class="identifier">End</span><span class="special">.</span>
</pre>
<p>
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#include</span> <span class="string">"stack4.hpp"</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">stack4</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">s</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">==</span> <span class="number">3</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">empty</span><span class="special">());</span>
    <span class="identifier">assert</span><span class="special">(!</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">full</span><span class="special">());</span>

    <span class="identifier">s</span><span class="special">.</span><span class="identifier">put</span><span class="special">(</span><span class="number">123</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(!</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">empty</span><span class="special">());</span>
    <span class="identifier">assert</span><span class="special">(!</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">full</span><span class="special">());</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">item</span><span class="special">()</span> <span class="special">==</span> <span class="number">123</span><span class="special">);</span>

    <span class="identifier">s</span><span class="special">.</span><span class="identifier">remove</span><span class="special">();</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">empty</span><span class="special">());</span>
    <span class="identifier">assert</span><span class="special">(!</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">full</span><span class="special">());</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
                </p>
              </td>
<td>
              </td>
</tr>
</tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__meyer97___stack3__error_codes_instead_of_preconditions"></a><a class="link" href="examples.html#boost_contract.examples.__meyer97___stack3__error_codes_instead_of_preconditions" title="[Meyer97] Stack3: Error codes instead of preconditions">[Meyer97]
      Stack3: Error codes instead of preconditions</a>
</h3></div></div></div>
<p>
</p>
<pre class="programlisting"><span class="comment">// File: stack3.cpp</span>
<span class="preprocessor">#include</span> <span class="string">"stack4.hpp"</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">optional</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="comment">// Dispenser LIFO with max capacity using error codes.</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">stack3</span> <span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">void</span> <span class="identifier">invariant</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="keyword">if</span><span class="special">(!</span><span class="identifier">error</span><span class="special">())</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span> <span class="comment">// Count non-negative.</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="identifier">capacity</span><span class="special">());</span> <span class="comment">// Count bounded.</span>
            <span class="comment">// Empty if no element.</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">empty</span><span class="special">()</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">));</span>
        <span class="special">}</span>
    <span class="special">}</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="keyword">enum</span> <span class="identifier">error_code</span> <span class="special">{</span>
        <span class="identifier">no_error</span> <span class="special">=</span> <span class="number">0</span><span class="special">,</span>
        <span class="identifier">overflow_error</span><span class="special">,</span>
        <span class="identifier">underflow_error</span><span class="special">,</span>
        <span class="identifier">size_error</span>
    <span class="special">};</span>

    <span class="comment">/* Initialization */</span>

    <span class="comment">// Create stack for max of n elems, if n &lt; 0 set error (no preconditions).</span>
    <span class="keyword">explicit</span> <span class="identifier">stack3</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">n</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">default_value</span> <span class="special">=</span> <span class="identifier">T</span><span class="special">())</span> <span class="special">:</span>
            <span class="identifier">stack_</span><span class="special">(</span><span class="number">0</span><span class="special">),</span> <span class="identifier">error_</span><span class="special">(</span><span class="identifier">no_error</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Error if impossible.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">((</span><span class="identifier">n</span> <span class="special">&lt;</span> <span class="number">0</span><span class="special">)</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">error</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">size_error</span><span class="special">));</span>
                <span class="comment">// No error if possible.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">((</span><span class="identifier">n</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">)</span> <span class="special">==</span> <span class="special">!</span><span class="identifier">error</span><span class="special">());</span>
                <span class="comment">// Created if no error.</span>
                <span class="keyword">if</span><span class="special">(!</span><span class="identifier">error</span><span class="special">())</span> <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">n</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">if</span><span class="special">(</span><span class="identifier">n</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">)</span> <span class="identifier">stack_</span> <span class="special">=</span> <span class="identifier">stack4</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;(</span><span class="identifier">n</span><span class="special">);</span>
        <span class="keyword">else</span> <span class="identifier">error_</span> <span class="special">=</span> <span class="identifier">size_error</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">/* Access */</span>

    <span class="comment">// Max number of stack elements.</span>
    <span class="keyword">int</span> <span class="identifier">capacity</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">stack_</span><span class="special">.</span><span class="identifier">capacity</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="comment">// Number of stack elements.</span>
    <span class="keyword">int</span> <span class="identifier">count</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">stack_</span><span class="special">.</span><span class="identifier">count</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="comment">// Top element if present, otherwise none and set error (no preconditions).</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;&gt;</span> <span class="identifier">item</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Error if impossible.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">empty</span><span class="special">()</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">error</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">underflow_error</span><span class="special">));</span>
                <span class="comment">// No error if possible.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">empty</span><span class="special">()</span> <span class="special">==</span> <span class="special">!</span><span class="identifier">error</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">if</span><span class="special">(!</span><span class="identifier">empty</span><span class="special">())</span> <span class="special">{</span>
            <span class="identifier">error_</span> <span class="special">=</span> <span class="identifier">no_error</span><span class="special">;</span>
            <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;&gt;(</span><span class="identifier">stack_</span><span class="special">.</span><span class="identifier">item</span><span class="special">());</span>
        <span class="special">}</span> <span class="keyword">else</span> <span class="special">{</span>
            <span class="identifier">error_</span> <span class="special">=</span> <span class="identifier">underflow_error</span><span class="special">;</span>
            <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;&gt;();</span>
        <span class="special">}</span>
    <span class="special">}</span>

    <span class="comment">/* Status Report */</span>

    <span class="comment">// Error indicator set by various operations.</span>
    <span class="identifier">error_code</span> <span class="identifier">error</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">error_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">bool</span> <span class="identifier">empty</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">stack_</span><span class="special">.</span><span class="identifier">empty</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="keyword">bool</span> <span class="identifier">full</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">stack_</span><span class="special">.</span><span class="identifier">full</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="comment">/* Element Change */</span>

    <span class="comment">// Add x to top if capacity allows, otherwise set error (no preconditions).</span>
    <span class="keyword">void</span> <span class="identifier">put</span><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">bool</span><span class="special">&gt;</span> <span class="identifier">old_full</span> <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">full</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">old_count</span> <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">count</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Error if impossible.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(*</span><span class="identifier">old_full</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">error</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">overflow_error</span><span class="special">));</span>
                <span class="comment">// No error if possible.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!*</span><span class="identifier">old_full</span> <span class="special">==</span> <span class="special">!</span><span class="identifier">error</span><span class="special">());</span>
                <span class="keyword">if</span><span class="special">(!</span><span class="identifier">error</span><span class="special">())</span> <span class="special">{</span> <span class="comment">// If no error...</span>
                    <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">empty</span><span class="special">());</span> <span class="comment">// ...not empty.</span>
                    <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(*</span><span class="identifier">item</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">x</span><span class="special">);</span> <span class="comment">// ...added to top.</span>
                    <span class="comment">// ...one more.</span>
                    <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_count</span> <span class="special">+</span> <span class="number">1</span><span class="special">);</span>
                <span class="special">}</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">if</span><span class="special">(</span><span class="identifier">full</span><span class="special">())</span> <span class="identifier">error_</span> <span class="special">=</span> <span class="identifier">overflow_error</span><span class="special">;</span>
        <span class="keyword">else</span> <span class="special">{</span>
            <span class="identifier">stack_</span><span class="special">.</span><span class="identifier">put</span><span class="special">(</span><span class="identifier">x</span><span class="special">);</span>
            <span class="identifier">error_</span> <span class="special">=</span> <span class="identifier">no_error</span><span class="special">;</span>
        <span class="special">}</span>
    <span class="special">}</span>

    <span class="comment">// Remove top element if possible, otherwise set error (no preconditions).</span>
    <span class="keyword">void</span> <span class="identifier">remove</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">bool</span><span class="special">&gt;</span> <span class="identifier">old_empty</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">empty</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">old_count</span> <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">count</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Error if impossible.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(*</span><span class="identifier">old_empty</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">error</span><span class="special">()</span> <span class="special">==</span>
                        <span class="identifier">underflow_error</span><span class="special">));</span>
                <span class="comment">// No error if possible.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!*</span><span class="identifier">old_empty</span> <span class="special">==</span> <span class="special">!</span><span class="identifier">error</span><span class="special">());</span>
                <span class="keyword">if</span><span class="special">(!</span><span class="identifier">error</span><span class="special">())</span> <span class="special">{</span> <span class="comment">// If no error...</span>
                    <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">full</span><span class="special">());</span> <span class="comment">// ...not full.</span>
                    <span class="comment">// ...one less.</span>
                    <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_count</span> <span class="special">-</span> <span class="number">1</span><span class="special">);</span>
                <span class="special">}</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">if</span><span class="special">(</span><span class="identifier">empty</span><span class="special">())</span> <span class="identifier">error_</span> <span class="special">=</span> <span class="identifier">underflow_error</span><span class="special">;</span>
        <span class="keyword">else</span> <span class="special">{</span>
            <span class="identifier">stack_</span><span class="special">.</span><span class="identifier">remove</span><span class="special">();</span>
            <span class="identifier">error_</span> <span class="special">=</span> <span class="identifier">no_error</span><span class="special">;</span>
        <span class="special">}</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="identifier">stack4</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">stack_</span><span class="special">;</span>
    <span class="keyword">mutable</span> <span class="identifier">error_code</span> <span class="identifier">error_</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">stack3</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">s</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">==</span> <span class="number">3</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">empty</span><span class="special">());</span>
    <span class="identifier">assert</span><span class="special">(!</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">full</span><span class="special">());</span>

    <span class="identifier">s</span><span class="special">.</span><span class="identifier">put</span><span class="special">(</span><span class="number">123</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(!</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">empty</span><span class="special">());</span>
    <span class="identifier">assert</span><span class="special">(!</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">full</span><span class="special">());</span>
    <span class="identifier">assert</span><span class="special">(*</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">item</span><span class="special">()</span> <span class="special">==</span> <span class="number">123</span><span class="special">);</span>

    <span class="identifier">s</span><span class="special">.</span><span class="identifier">remove</span><span class="special">();</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">empty</span><span class="special">());</span>
    <span class="identifier">assert</span><span class="special">(!</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">full</span><span class="special">());</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__mitchell02___name_list__relaxed_subcontracts"></a><a class="link" href="examples.html#boost_contract.examples.__mitchell02___name_list__relaxed_subcontracts" title="[Mitchell02] Name List: Relaxed subcontracts">[Mitchell02]
      Name List: Relaxed subcontracts</a>
</h3></div></div></div>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">algorithm</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="comment">// List of names.</span>
<span class="keyword">class</span> <span class="identifier">name_list</span> <span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">void</span> <span class="identifier">invariant</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span> <span class="comment">// Non-negative count.</span>
    <span class="special">}</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="comment">/* Creation */</span>

    <span class="comment">// Create an empty list.</span>
    <span class="identifier">name_list</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span> <span class="comment">// Empty list.</span>
            <span class="special">})</span>
        <span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">// Destroy list.</span>
    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">name_list</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">/* Basic Queries */</span>

    <span class="comment">// Number of names in list.</span>
    <span class="keyword">int</span> <span class="identifier">count</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">names_</span><span class="special">.</span><span class="identifier">size</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="comment">// Is name in list?</span>
    <span class="keyword">bool</span> <span class="identifier">has</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">name</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="keyword">bool</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// If empty, has not.</span>
                <span class="keyword">if</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">)</span> <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">result</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">names_</span><span class="special">.</span><span class="identifier">cend</span><span class="special">()</span> <span class="special">!=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">find</span><span class="special">(</span><span class="identifier">names_</span><span class="special">.</span><span class="identifier">cbegin</span><span class="special">(),</span>
                <span class="identifier">names_</span><span class="special">.</span><span class="identifier">cend</span><span class="special">(),</span> <span class="identifier">name</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">/* Commands */</span>

    <span class="comment">// Add name to list, if name not already in list.</span>
    <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">put</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">name</span><span class="special">,</span>
            <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">bool</span><span class="special">&gt;</span> <span class="identifier">old_has_name</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">has</span><span class="special">(</span><span class="identifier">name</span><span class="special">));</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">old_count</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">count</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">has</span><span class="special">(</span><span class="identifier">name</span><span class="special">));</span> <span class="comment">// Not already in list.</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="keyword">if</span><span class="special">(!*</span><span class="identifier">old_has_name</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// If-guard allows to relax subcontracts.</span>
                    <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">has</span><span class="special">(</span><span class="identifier">name</span><span class="special">));</span> <span class="comment">// Name in list.</span>
                    <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_count</span> <span class="special">+</span> <span class="number">1</span><span class="special">);</span> <span class="comment">// Inc.</span>
                <span class="special">}</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">names_</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">name</span><span class="special">);</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;</span> <span class="identifier">names_</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">class</span> <span class="identifier">relaxed_name_list</span>
    <span class="preprocessor">#define</span> <span class="identifier">BASES</span> <span class="keyword">public</span> <span class="identifier">name_list</span>
    <span class="special">:</span> <span class="identifier">BASES</span>
<span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">typedef</span> <span class="identifier">BOOST_CONTRACT_BASE_TYPES</span><span class="special">(</span><span class="identifier">BASES</span><span class="special">)</span> <span class="identifier">base_types</span><span class="special">;</span> <span class="comment">// Subcontracting.</span>
    <span class="preprocessor">#undef</span> <span class="identifier">BASES</span>

    <span class="identifier">BOOST_CONTRACT_OVERRIDE</span><span class="special">(</span><span class="identifier">put</span><span class="special">);</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="comment">/*  Commands */</span>

    <span class="comment">// Add name to list, or do nothing if name already in list (relaxed).</span>
    <span class="keyword">void</span> <span class="identifier">put</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">name</span><span class="special">,</span>
            <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="comment">/* override */</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">bool</span><span class="special">&gt;</span> <span class="identifier">old_has_name</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">has</span><span class="special">(</span><span class="identifier">name</span><span class="special">));</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">old_count</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">count</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">&lt;</span>
                <span class="identifier">override_put</span><span class="special">&gt;(</span><span class="identifier">v</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">relaxed_name_list</span><span class="special">::</span><span class="identifier">put</span><span class="special">,</span> <span class="keyword">this</span><span class="special">,</span> <span class="identifier">name</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span> <span class="comment">// Relax inherited preconditions.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">has</span><span class="special">(</span><span class="identifier">name</span><span class="special">));</span> <span class="comment">// Already in list.</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span> <span class="comment">// Inherited post. not checked given if-guard.</span>
                <span class="keyword">if</span><span class="special">(*</span><span class="identifier">old_has_name</span><span class="special">)</span> <span class="special">{</span>
                    <span class="comment">// Count unchanged if name already in list.</span>
                    <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_count</span><span class="special">);</span>
                <span class="special">}</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">if</span><span class="special">(!</span><span class="identifier">has</span><span class="special">(</span><span class="identifier">name</span><span class="special">))</span> <span class="identifier">name_list</span><span class="special">::</span><span class="identifier">put</span><span class="special">(</span><span class="identifier">name</span><span class="special">);</span> <span class="comment">// Else, do nothing.</span>
    <span class="special">}</span>
<span class="special">};</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span> <span class="identifier">js</span> <span class="special">=</span> <span class="string">"John Smith"</span><span class="special">;</span>

    <span class="identifier">relaxed_name_list</span> <span class="identifier">rl</span><span class="special">;</span>
    <span class="identifier">rl</span><span class="special">.</span><span class="identifier">put</span><span class="special">(</span><span class="identifier">js</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">rl</span><span class="special">.</span><span class="identifier">has</span><span class="special">(</span><span class="identifier">js</span><span class="special">));</span>
    <span class="identifier">rl</span><span class="special">.</span><span class="identifier">put</span><span class="special">(</span><span class="identifier">js</span><span class="special">);</span> <span class="comment">// OK, relaxed contracts allow calling this again (do nothing).</span>

    <span class="identifier">name_list</span> <span class="identifier">nl</span><span class="special">;</span>
    <span class="identifier">nl</span><span class="special">.</span><span class="identifier">put</span><span class="special">(</span><span class="identifier">js</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">nl</span><span class="special">.</span><span class="identifier">has</span><span class="special">(</span><span class="identifier">js</span><span class="special">));</span>
    <span class="comment">// nl.put(js); // Error, contracts do not allow calling this again.</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__mitchell02___dictionary__key_value_map"></a><a class="link" href="examples.html#boost_contract.examples.__mitchell02___dictionary__key_value_map" title="[Mitchell02] Dictionary: Key-value map">[Mitchell02]
      Dictionary: Key-value map</a>
</h3></div></div></div>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">utility</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">map</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">K</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">dictionary</span> <span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">void</span> <span class="identifier">invariant</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span> <span class="comment">// Non-negative count.</span>
    <span class="special">}</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="comment">/* Creation */</span>

    <span class="comment">// Create empty dictionary.</span>
    <span class="identifier">dictionary</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span> <span class="comment">// Empty.</span>
            <span class="special">})</span>
        <span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">// Destroy dictionary.</span>
    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">dictionary</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">/* Basic Queries */</span>

    <span class="comment">// Number of key entries.</span>
    <span class="keyword">int</span> <span class="identifier">count</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">items_</span><span class="special">.</span><span class="identifier">size</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="comment">// Has entry for key?</span>
    <span class="keyword">bool</span> <span class="identifier">has</span><span class="special">(</span><span class="identifier">K</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">key</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="keyword">bool</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Empty has no key.</span>
                <span class="keyword">if</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">)</span> <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">result</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">items_</span><span class="special">.</span><span class="identifier">find</span><span class="special">(</span><span class="identifier">key</span><span class="special">)</span> <span class="special">!=</span> <span class="identifier">items_</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span>
    <span class="special">}</span>

    <span class="comment">// Value for a given key.</span>
    <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value_for</span><span class="special">(</span><span class="identifier">K</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">key</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">has</span><span class="special">(</span><span class="identifier">key</span><span class="special">));</span> <span class="comment">// Has key.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="comment">// Find != end because of precondition (no defensive programming).</span>
        <span class="keyword">return</span> <span class="identifier">items_</span><span class="special">.</span><span class="identifier">find</span><span class="special">(</span><span class="identifier">key</span><span class="special">)-&gt;</span><span class="identifier">second</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">/* Commands */</span>

    <span class="comment">// Add value of a given key.</span>
    <span class="keyword">void</span> <span class="identifier">put</span><span class="special">(</span><span class="identifier">K</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">key</span><span class="special">,</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">old_count</span> <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">count</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">has</span><span class="special">(</span><span class="identifier">key</span><span class="special">));</span> <span class="comment">// Has not key already.</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_count</span> <span class="special">+</span> <span class="number">1</span><span class="special">);</span> <span class="comment">// Count inc.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">has</span><span class="special">(</span><span class="identifier">key</span><span class="special">));</span> <span class="comment">// Has key.</span>
                <span class="comment">// Value set for key.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">value_for</span><span class="special">(</span><span class="identifier">key</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">value</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">items_</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">make_pair</span><span class="special">(</span><span class="identifier">key</span><span class="special">,</span> <span class="identifier">value</span><span class="special">));</span>
    <span class="special">}</span>

    <span class="comment">// Remove value for given key.</span>
    <span class="keyword">void</span> <span class="identifier">remove</span><span class="special">(</span><span class="identifier">K</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">key</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">old_count</span> <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">count</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">has</span><span class="special">(</span><span class="identifier">key</span><span class="special">));</span> <span class="comment">// Has key.</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_count</span> <span class="special">-</span> <span class="number">1</span><span class="special">);</span> <span class="comment">// Count dec.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">has</span><span class="special">(</span><span class="identifier">key</span><span class="special">));</span> <span class="comment">// Has not key.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">items_</span><span class="special">.</span><span class="identifier">erase</span><span class="special">(</span><span class="identifier">key</span><span class="special">);</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">map</span><span class="special">&lt;</span><span class="identifier">K</span><span class="special">,</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">items_</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span> <span class="identifier">js</span> <span class="special">=</span> <span class="string">"John Smith"</span><span class="special">;</span>

    <span class="identifier">dictionary</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">ages</span><span class="special">;</span>
    <span class="identifier">assert</span><span class="special">(!</span><span class="identifier">ages</span><span class="special">.</span><span class="identifier">has</span><span class="special">(</span><span class="identifier">js</span><span class="special">));</span>

    <span class="identifier">ages</span><span class="special">.</span><span class="identifier">put</span><span class="special">(</span><span class="identifier">js</span><span class="special">,</span> <span class="number">23</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">ages</span><span class="special">.</span><span class="identifier">value_for</span><span class="special">(</span><span class="identifier">js</span><span class="special">)</span> <span class="special">==</span> <span class="number">23</span><span class="special">);</span>

    <span class="identifier">ages</span><span class="special">.</span><span class="identifier">remove</span><span class="special">(</span><span class="identifier">js</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">ages</span><span class="special">.</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__mitchell02___courier__subcontracting_and_static_class_invariants"></a><a class="link" href="examples.html#boost_contract.examples.__mitchell02___courier__subcontracting_and_static_class_invariants" title="[Mitchell02] Courier: Subcontracting and static class invariants">[Mitchell02]
      Courier: Subcontracting and static class invariants</a>
</h3></div></div></div>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="keyword">struct</span> <span class="identifier">package</span> <span class="special">{</span>
    <span class="keyword">double</span> <span class="identifier">weight_kg</span><span class="special">;</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">location</span><span class="special">;</span>
    <span class="keyword">double</span> <span class="identifier">accepted_hour</span><span class="special">;</span>
    <span class="keyword">double</span> <span class="identifier">delivered_hour</span><span class="special">;</span>

    <span class="keyword">explicit</span> <span class="identifier">package</span><span class="special">(</span>
        <span class="keyword">double</span> <span class="identifier">_weight_kg</span><span class="special">,</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">_location</span> <span class="special">=</span> <span class="string">""</span><span class="special">,</span>
        <span class="keyword">double</span> <span class="identifier">_accepted_hour</span> <span class="special">=</span> <span class="number">0.0</span><span class="special">,</span>
        <span class="keyword">double</span> <span class="identifier">_delivered_hour</span> <span class="special">=</span> <span class="number">0.0</span>
    <span class="special">)</span> <span class="special">:</span>
        <span class="identifier">weight_kg</span><span class="special">(</span><span class="identifier">_weight_kg</span><span class="special">),</span>
        <span class="identifier">location</span><span class="special">(</span><span class="identifier">_location</span><span class="special">),</span>
        <span class="identifier">accepted_hour</span><span class="special">(</span><span class="identifier">_accepted_hour</span><span class="special">),</span>
        <span class="identifier">delivered_hour</span><span class="special">(</span><span class="identifier">_delivered_hour</span><span class="special">)</span>
    <span class="special">{}</span>
<span class="special">};</span>

<span class="comment">// Courier for package delivery.</span>
<span class="keyword">class</span> <span class="identifier">courier</span>
    <span class="preprocessor">#define</span> <span class="identifier">BASES</span> <span class="keyword">private</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor_precondition</span><span class="special">&lt;</span><span class="identifier">courier</span><span class="special">&gt;</span>
    <span class="special">:</span> <span class="identifier">BASES</span>
<span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">typedef</span> <span class="identifier">BOOST_CONTRACT_BASE_TYPES</span><span class="special">(</span><span class="identifier">BASES</span><span class="special">)</span> <span class="identifier">base_types</span><span class="special">;</span>
    <span class="preprocessor">#undef</span> <span class="identifier">BASES</span>

    <span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">static_invariant</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Positive min. insurance.</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">min_insurance_usd</span> <span class="special">&gt;=</span> <span class="number">0.0</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">void</span> <span class="identifier">invariant</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Above min. insurance.</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">insurance_cover_usd</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">min_insurance_usd</span><span class="special">);</span>
    <span class="special">}</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="keyword">static</span> <span class="keyword">double</span> <span class="identifier">min_insurance_usd</span><span class="special">;</span>

    <span class="comment">/* Creation */</span>

    <span class="comment">// Create courier with specified insurance value.</span>
    <span class="keyword">explicit</span> <span class="identifier">courier</span><span class="special">(</span><span class="keyword">double</span> <span class="identifier">_insurance_cover_usd</span> <span class="special">=</span> <span class="identifier">min_insurance_usd</span><span class="special">)</span> <span class="special">:</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor_precondition</span><span class="special">&lt;</span><span class="identifier">courier</span><span class="special">&gt;([&amp;]</span> <span class="special">{</span>
            <span class="comment">// Positive insurance.</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">_insurance_cover_usd</span> <span class="special">&gt;=</span> <span class="number">0.0</span><span class="special">);</span>
        <span class="special">}),</span>
        <span class="identifier">insurance_cover_usd_</span><span class="special">(</span><span class="identifier">_insurance_cover_usd</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">// Destroy courier.</span>
    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">courier</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">/* Queries */</span>

    <span class="comment">// Return insurance cover.</span>
    <span class="keyword">double</span> <span class="identifier">insurance_cover_usd</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">insurance_cover_usd_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">/* Commands */</span>

    <span class="comment">// Deliver package to destination.</span>
    <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">deliver</span><span class="special">(</span>
        <span class="identifier">package</span><span class="special">&amp;</span> <span class="identifier">package_delivery</span><span class="special">,</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">destination</span><span class="special">,</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span>
    <span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Within max weight of this delivery.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">package_delivery</span><span class="special">.</span><span class="identifier">weight_kg</span> <span class="special">&lt;</span> <span class="number">5.0</span><span class="special">);</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Within max delivery type.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="keyword">double</span><span class="special">(</span><span class="identifier">package_delivery</span><span class="special">.</span><span class="identifier">delivered_hour</span> <span class="special">-</span>
                        <span class="identifier">package_delivery</span><span class="special">.</span><span class="identifier">accepted_hour</span><span class="special">)</span> <span class="special">&lt;=</span> <span class="number">3.0</span><span class="special">);</span>
                <span class="comment">// Delivered at destination.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">package_delivery</span><span class="special">.</span><span class="identifier">location</span> <span class="special">==</span> <span class="identifier">destination</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">package_delivery</span><span class="special">.</span><span class="identifier">location</span> <span class="special">=</span> <span class="identifier">destination</span><span class="special">;</span>
        <span class="comment">// Delivery takes 2.5 hours.</span>
        <span class="identifier">package_delivery</span><span class="special">.</span><span class="identifier">delivered_hour</span> <span class="special">=</span> <span class="identifier">package_delivery</span><span class="special">.</span><span class="identifier">accepted_hour</span> <span class="special">+</span> <span class="number">2.5</span><span class="special">;</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="keyword">double</span> <span class="identifier">insurance_cover_usd_</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">double</span> <span class="identifier">courier</span><span class="special">::</span><span class="identifier">min_insurance_usd</span> <span class="special">=</span> <span class="number">10.0e+6</span><span class="special">;</span>

<span class="comment">// Different courier for package delivery.</span>
<span class="keyword">class</span> <span class="identifier">different_courier</span>
    <span class="preprocessor">#define</span> <span class="identifier">BASES</span> <span class="keyword">private</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor_precondition</span><span class="special">&lt;</span> <span class="special">\</span>
            <span class="identifier">different_courier</span><span class="special">&gt;,</span> <span class="keyword">public</span> <span class="identifier">courier</span>
    <span class="special">:</span> <span class="identifier">BASES</span>
<span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">typedef</span> <span class="identifier">BOOST_CONTRACT_BASE_TYPES</span><span class="special">(</span><span class="identifier">BASES</span><span class="special">)</span> <span class="identifier">base_types</span><span class="special">;</span> <span class="comment">// Subcontracting.</span>
    <span class="preprocessor">#undef</span> <span class="identifier">BASES</span>

    <span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">static_invariant</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span> <span class="comment">// Better insurance amount.</span>
                <span class="identifier">different_insurance_usd</span> <span class="special">&gt;=</span> <span class="identifier">courier</span><span class="special">::</span><span class="identifier">min_insurance_usd</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">void</span> <span class="identifier">invariant</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Above different insurance value.</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">insurance_cover_usd</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">different_insurance_usd</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="identifier">BOOST_CONTRACT_OVERRIDE</span><span class="special">(</span><span class="identifier">deliver</span><span class="special">)</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="keyword">static</span> <span class="keyword">double</span> <span class="identifier">different_insurance_usd</span><span class="special">;</span>

    <span class="comment">/* Creation */</span>

    <span class="comment">// Create courier with specified insurance value.</span>
    <span class="keyword">explicit</span> <span class="identifier">different_courier</span><span class="special">(</span>
            <span class="keyword">double</span> <span class="identifier">insurance_cover_usd</span> <span class="special">=</span> <span class="identifier">different_insurance_usd</span><span class="special">)</span> <span class="special">:</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor_precondition</span><span class="special">&lt;</span><span class="identifier">different_courier</span><span class="special">&gt;([&amp;]</span> <span class="special">{</span>
            <span class="comment">// Positive insurance value.</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">insurance_cover_usd</span> <span class="special">&gt;</span> <span class="number">0.0</span><span class="special">);</span>
        <span class="special">}),</span>
        <span class="identifier">courier</span><span class="special">(</span><span class="identifier">insurance_cover_usd</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">// Destroy courier.</span>
    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">different_courier</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">/* Commands */</span>

    <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">deliver</span><span class="special">(</span>
        <span class="identifier">package</span><span class="special">&amp;</span> <span class="identifier">package_delivery</span><span class="special">,</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">destination</span><span class="special">,</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span>
    <span class="special">)</span> <span class="comment">/* override */</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">&lt;</span>
            <span class="identifier">override_deliver</span>
        <span class="special">&gt;(</span><span class="identifier">v</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">different_courier</span><span class="special">::</span><span class="identifier">deliver</span><span class="special">,</span> <span class="keyword">this</span><span class="special">,</span> <span class="identifier">package_delivery</span><span class="special">,</span> <span class="identifier">destination</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Package can weight more (weaker precondition).</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">package_delivery</span><span class="special">.</span><span class="identifier">weight_kg</span> <span class="special">&lt;=</span> <span class="number">8.0</span><span class="special">);</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Faster delivery (stronger postcondition).</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="keyword">double</span><span class="special">(</span><span class="identifier">package_delivery</span><span class="special">.</span><span class="identifier">delivered_hour</span> <span class="special">-</span>
                        <span class="identifier">package_delivery</span><span class="special">.</span><span class="identifier">accepted_hour</span><span class="special">)</span> <span class="special">&lt;=</span> <span class="number">2.0</span><span class="special">);</span>
                <span class="comment">// Inherited "delivery at destination" postcondition.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">package_delivery</span><span class="special">.</span><span class="identifier">location</span> <span class="special">=</span> <span class="identifier">destination</span><span class="special">;</span>
        <span class="comment">// Delivery takes 0.5 hours.</span>
        <span class="identifier">package_delivery</span><span class="special">.</span><span class="identifier">delivered_hour</span> <span class="special">=</span> <span class="identifier">package_delivery</span><span class="special">.</span><span class="identifier">accepted_hour</span> <span class="special">+</span> <span class="number">0.5</span><span class="special">;</span>
    <span class="special">}</span>
<span class="special">};</span>

<span class="keyword">double</span> <span class="identifier">different_courier</span><span class="special">::</span><span class="identifier">different_insurance_usd</span> <span class="special">=</span> <span class="number">20.0e+6</span><span class="special">;</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">package</span> <span class="identifier">cups</span><span class="special">(</span><span class="number">3.6</span><span class="special">,</span> <span class="string">"store"</span><span class="special">);</span>
    <span class="identifier">courier</span> <span class="identifier">c</span><span class="special">;</span>
    <span class="identifier">c</span><span class="special">.</span><span class="identifier">deliver</span><span class="special">(</span><span class="identifier">cups</span><span class="special">,</span> <span class="string">"home"</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">cups</span><span class="special">.</span><span class="identifier">location</span> <span class="special">==</span> <span class="string">"home"</span><span class="special">);</span>

    <span class="identifier">package</span> <span class="identifier">desk</span><span class="special">(</span><span class="number">7.2</span><span class="special">,</span> <span class="string">"store"</span><span class="special">);</span>
    <span class="identifier">different_courier</span> <span class="identifier">dc</span><span class="special">;</span>
    <span class="identifier">dc</span><span class="special">.</span><span class="identifier">deliver</span><span class="special">(</span><span class="identifier">desk</span><span class="special">,</span> <span class="string">"office"</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">desk</span><span class="special">.</span><span class="identifier">location</span> <span class="special">==</span> <span class="string">"office"</span><span class="special">);</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__mitchell02___stack__stack_like_container"></a><a class="link" href="examples.html#boost_contract.examples.__mitchell02___stack__stack_like_container" title="[Mitchell02] Stack: Stack-like container">[Mitchell02]
      Stack: Stack-like container</a>
</h3></div></div></div>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">optional</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">stack</span> <span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">void</span> <span class="identifier">invariant</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span> <span class="comment">// Non-negative count.</span>
    <span class="special">}</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="comment">/* Creation */</span>

    <span class="comment">// Create empty stack.</span>
    <span class="identifier">stack</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span> <span class="comment">// Empty.</span>
            <span class="special">})</span>
        <span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">// Destroy stack.</span>
    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">stack</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">/* Basic Queries */</span>

    <span class="comment">// Number of items.</span>
    <span class="keyword">int</span> <span class="identifier">count</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">items_</span><span class="special">.</span><span class="identifier">size</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="comment">// Item at index in [1, count()] (as in Eiffel).</span>
    <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">item_at</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">index</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">index</span> <span class="special">&gt;</span> <span class="number">0</span><span class="special">);</span> <span class="comment">// Positive index.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">index</span> <span class="special">&lt;=</span> <span class="identifier">count</span><span class="special">());</span> <span class="comment">// Index within count.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">items_</span><span class="special">[</span><span class="identifier">index</span> <span class="special">-</span> <span class="number">1</span><span class="special">];</span>
    <span class="special">}</span>

    <span class="comment">/* Derived Queries */</span>

    <span class="comment">// If no items.</span>
    <span class="keyword">bool</span> <span class="identifier">is_empty</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="keyword">bool</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Consistent with count.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">// Top item.</span>
    <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">item</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;&gt;</span> <span class="identifier">result</span><span class="special">;</span> <span class="comment">// Avoid extra construction of T.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">&gt;</span> <span class="number">0</span><span class="special">);</span> <span class="comment">// Not empty.</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Item on top.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(*</span><span class="identifier">result</span> <span class="special">==</span> <span class="identifier">item_at</span><span class="special">(</span><span class="identifier">count</span><span class="special">()));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="special">*(</span><span class="identifier">result</span> <span class="special">=</span> <span class="identifier">item_at</span><span class="special">(</span><span class="identifier">count</span><span class="special">()));</span>
    <span class="special">}</span>

    <span class="comment">/* Commands */</span>

    <span class="comment">// Push item to the top.</span>
    <span class="keyword">void</span> <span class="identifier">put</span><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">new_item</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">old_count</span> <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">count</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_count</span> <span class="special">+</span> <span class="number">1</span><span class="special">);</span> <span class="comment">// Count inc.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">item</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">new_item</span><span class="special">);</span> <span class="comment">// Item set.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">items_</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">new_item</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">// Pop top item.</span>
    <span class="keyword">void</span> <span class="identifier">remove</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">old_count</span> <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">count</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">&gt;</span> <span class="number">0</span><span class="special">);</span> <span class="comment">// Not empty.</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_count</span> <span class="special">-</span> <span class="number">1</span><span class="special">);</span> <span class="comment">// Count dec.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">items_</span><span class="special">.</span><span class="identifier">pop_back</span><span class="special">();</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">items_</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">stack</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">s</span><span class="special">;</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>

    <span class="identifier">s</span><span class="special">.</span><span class="identifier">put</span><span class="special">(</span><span class="number">123</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">item</span><span class="special">()</span> <span class="special">==</span> <span class="number">123</span><span class="special">);</span>

    <span class="identifier">s</span><span class="special">.</span><span class="identifier">remove</span><span class="special">();</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">is_empty</span><span class="special">());</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__mitchell02___simple_queue__queue_like_container_and_disable_old_value_copies_for_audit_assertions"></a><a class="link" href="examples.html#boost_contract.examples.__mitchell02___simple_queue__queue_like_container_and_disable_old_value_copies_for_audit_assertions" title="[Mitchell02] Simple Queue: Queue-like container and disable old value copies for audit assertions">[Mitchell02]
      Simple Queue: Queue-like container and disable old value copies for audit assertions</a>
</h3></div></div></div>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">optional</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">simple_queue</span>
    <span class="preprocessor">#define</span> <span class="identifier">BASES</span> <span class="keyword">private</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor_precondition</span><span class="special">&lt;</span> <span class="special">\</span>
            <span class="identifier">simple_queue</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;</span>
    <span class="special">:</span> <span class="identifier">BASES</span>
<span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">typedef</span> <span class="identifier">BOOST_CONTRACT_BASE_TYPES</span><span class="special">(</span><span class="identifier">BASES</span><span class="special">)</span> <span class="identifier">base_types</span><span class="special">;</span>
    <span class="preprocessor">#undef</span> <span class="identifier">BASES</span>

    <span class="keyword">void</span> <span class="identifier">invariant</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span> <span class="comment">// Non-negative count.</span>
    <span class="special">}</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="comment">/* Creation */</span>

    <span class="comment">// Create empty queue.</span>
    <span class="keyword">explicit</span> <span class="identifier">simple_queue</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">a_capacity</span><span class="special">)</span> <span class="special">:</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor_precondition</span><span class="special">&lt;</span><span class="identifier">simple_queue</span><span class="special">&gt;([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">a_capacity</span> <span class="special">&gt;</span> <span class="number">0</span><span class="special">);</span> <span class="comment">// Positive capacity.</span>
        <span class="special">})</span>
    <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Capacity set.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">a_capacity</span><span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">is_empty</span><span class="special">());</span> <span class="comment">// Empty.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">items_</span><span class="special">.</span><span class="identifier">reserve</span><span class="special">(</span><span class="identifier">a_capacity</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">// Destroy queue.</span>
    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">simple_queue</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">/* Basic Queries */</span>

    <span class="comment">// Items in queue (in their order).</span>
    <span class="comment">// (Somewhat exposes implementation but allows to check more contracts.)</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">items</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">items_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">// Max number of items queue can hold.</span>
    <span class="keyword">int</span> <span class="identifier">capacity</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">items_</span><span class="special">.</span><span class="identifier">capacity</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="comment">/* Derived Queries */</span>

    <span class="comment">// Number of items.</span>
    <span class="keyword">int</span> <span class="identifier">count</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="keyword">int</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Return items count.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="keyword">int</span><span class="special">(</span><span class="identifier">items</span><span class="special">().</span><span class="identifier">size</span><span class="special">()));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">items_</span><span class="special">.</span><span class="identifier">size</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="comment">// Item at head.</span>
    <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">head</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;&gt;</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">is_empty</span><span class="special">());</span> <span class="comment">// Not empty.</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Return item on top.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(*</span><span class="identifier">result</span> <span class="special">==</span> <span class="identifier">items</span><span class="special">().</span><span class="identifier">at</span><span class="special">(</span><span class="number">0</span><span class="special">));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="special">*(</span><span class="identifier">result</span> <span class="special">=</span> <span class="identifier">items_</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="number">0</span><span class="special">));</span>
    <span class="special">}</span>

    <span class="comment">// If queue contains no item.</span>
    <span class="keyword">bool</span> <span class="identifier">is_empty</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="keyword">bool</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Consistent with count.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">items_</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">// If queue has no room for another item.</span>
    <span class="keyword">bool</span> <span class="identifier">is_full</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="keyword">bool</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span> <span class="comment">// Consistent with size and capacity.</span>
                        <span class="identifier">result</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">==</span> <span class="keyword">int</span><span class="special">(</span><span class="identifier">items</span><span class="special">().</span><span class="identifier">size</span><span class="special">())));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">items_</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">items_</span><span class="special">.</span><span class="identifier">capacity</span><span class="special">());</span>
    <span class="special">}</span>

    <span class="comment">/* Commands */</span>

    <span class="comment">// Remove head itme and shift all other items.</span>
    <span class="keyword">void</span> <span class="identifier">remove</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Expensive all_equal postcond. and old_items copy might be skipped.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">old_items</span><span class="special">;</span>
            <span class="preprocessor">#ifdef</span> <span class="identifier">BOOST_CONTRACT_AUDIITS</span>
                <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">items</span><span class="special">())</span>
            <span class="preprocessor">#endif</span> <span class="comment">// Else, leave old pointer null...</span>
        <span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">old_count</span> <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">count</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">is_empty</span><span class="special">());</span> <span class="comment">// Not empty.</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_count</span> <span class="special">-</span> <span class="number">1</span><span class="special">);</span> <span class="comment">// Count dec.</span>
                <span class="comment">// ...following skipped #ifndef AUDITS.</span>
                <span class="keyword">if</span><span class="special">(</span><span class="identifier">old_items</span><span class="special">)</span> <span class="identifier">all_equal</span><span class="special">(</span><span class="identifier">items</span><span class="special">(),</span> <span class="special">*</span><span class="identifier">old_items</span><span class="special">,</span> <span class="comment">/* shifted = */</span> <span class="number">1</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">items_</span><span class="special">.</span><span class="identifier">erase</span><span class="special">(</span><span class="identifier">items_</span><span class="special">.</span><span class="identifier">begin</span><span class="special">());</span>
    <span class="special">}</span>

    <span class="comment">// Add item to tail.</span>
    <span class="keyword">void</span> <span class="identifier">put</span><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">item</span><span class="special">)</span> <span class="special">{</span>
        <span class="comment">// Expensive all_equal postcond. and old_items copy might be skipped.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">old_items</span><span class="special">;</span>
            <span class="preprocessor">#ifdef</span> <span class="identifier">BOOST_CONTRACT_AUDITS</span>
                <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">items</span><span class="special">())</span>
            <span class="preprocessor">#endif</span> <span class="comment">// Else, leave old pointer null...</span>
        <span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">old_count</span> <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">count</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">&lt;</span> <span class="identifier">capacity</span><span class="special">());</span> <span class="comment">// Room for add.</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_count</span> <span class="special">+</span> <span class="number">1</span><span class="special">);</span> <span class="comment">// Count inc.</span>
                <span class="comment">// Second to last item.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">items</span><span class="special">().</span><span class="identifier">at</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">-</span> <span class="number">1</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">item</span><span class="special">);</span>
                <span class="comment">// ...following skipped #ifndef AUDITS.</span>
                <span class="keyword">if</span><span class="special">(</span><span class="identifier">old_items</span><span class="special">)</span> <span class="identifier">all_equal</span><span class="special">(</span><span class="identifier">items</span><span class="special">(),</span> <span class="special">*</span><span class="identifier">old_items</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">items_</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">item</span><span class="special">);</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="comment">// Contract helper.</span>
    <span class="keyword">static</span> <span class="keyword">bool</span> <span class="identifier">all_equal</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">left</span><span class="special">,</span>
            <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">right</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">offset</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">function</span><span class="special">()</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Correct offset.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">right</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">left</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">+</span> <span class="identifier">offset</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="identifier">offset</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">right</span><span class="special">.</span><span class="identifier">size</span><span class="special">();</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
            <span class="keyword">if</span><span class="special">(</span><span class="identifier">left</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="identifier">i</span> <span class="special">-</span> <span class="identifier">offset</span><span class="special">)</span> <span class="special">!=</span> <span class="identifier">right</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="identifier">i</span><span class="special">))</span> <span class="keyword">return</span> <span class="keyword">false</span><span class="special">;</span>
        <span class="special">}</span>
        <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">items_</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">simple_queue</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">q</span><span class="special">(</span><span class="number">10</span><span class="special">);</span>
    <span class="identifier">q</span><span class="special">.</span><span class="identifier">put</span><span class="special">(</span><span class="number">123</span><span class="special">);</span>
    <span class="identifier">q</span><span class="special">.</span><span class="identifier">put</span><span class="special">(</span><span class="number">456</span><span class="special">);</span>

    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">q</span><span class="special">.</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">==</span> <span class="number">10</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">q</span><span class="special">.</span><span class="identifier">head</span><span class="special">()</span> <span class="special">==</span> <span class="number">123</span><span class="special">);</span>

    <span class="identifier">assert</span><span class="special">(!</span><span class="identifier">q</span><span class="special">.</span><span class="identifier">is_empty</span><span class="special">());</span>
    <span class="identifier">assert</span><span class="special">(!</span><span class="identifier">q</span><span class="special">.</span><span class="identifier">is_full</span><span class="special">());</span>

    <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">items</span> <span class="special">=</span> <span class="identifier">q</span><span class="special">.</span><span class="identifier">items</span><span class="special">();</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">items</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="number">0</span><span class="special">)</span> <span class="special">==</span> <span class="number">123</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">items</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special">==</span> <span class="number">456</span><span class="special">);</span>

    <span class="identifier">q</span><span class="special">.</span><span class="identifier">remove</span><span class="special">();</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">q</span><span class="special">.</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="number">1</span><span class="special">);</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__mitchell02___customer_manager__contracts_instead_of_defensive_programming"></a><a class="link" href="examples.html#boost_contract.examples.__mitchell02___customer_manager__contracts_instead_of_defensive_programming" title="[Mitchell02] Customer Manager: Contracts instead of defensive programming">[Mitchell02]
      Customer Manager: Contracts instead of defensive programming</a>
</h3></div></div></div>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">map</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">utility</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="comment">// Basic customer information.</span>
<span class="keyword">struct</span> <span class="identifier">customer_info</span> <span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">customer_manager</span><span class="special">;</span>

    <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">identifier</span><span class="special">;</span>

    <span class="identifier">identifier</span> <span class="identifier">id</span><span class="special">;</span>

    <span class="keyword">explicit</span> <span class="identifier">customer_info</span><span class="special">(</span><span class="identifier">identifier</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">_id</span><span class="special">)</span> <span class="special">:</span>
            <span class="identifier">id</span><span class="special">(</span><span class="identifier">_id</span><span class="special">),</span> <span class="identifier">name_</span><span class="special">(),</span> <span class="identifier">address_</span><span class="special">(),</span> <span class="identifier">birthday_</span><span class="special">()</span> <span class="special">{}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">name_</span><span class="special">;</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">address_</span><span class="special">;</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">birthday_</span><span class="special">;</span>
<span class="special">};</span>

<span class="comment">// Manage customers.</span>
<span class="keyword">class</span> <span class="identifier">customer_manager</span> <span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">void</span> <span class="identifier">invariant</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span> <span class="comment">// Non-negative count.</span>
    <span class="special">}</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="comment">/* Creation */</span>

    <span class="identifier">customer_manager</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">customer_manager</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">/* Basic Queries */</span>

    <span class="keyword">int</span> <span class="identifier">count</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">customers_</span><span class="special">.</span><span class="identifier">size</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="keyword">bool</span> <span class="identifier">id_active</span><span class="special">(</span><span class="identifier">customer_info</span><span class="special">::</span><span class="identifier">identifier</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">id</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">customers_</span><span class="special">.</span><span class="identifier">find</span><span class="special">(</span><span class="identifier">id</span><span class="special">)</span> <span class="special">!=</span> <span class="identifier">customers_</span><span class="special">.</span><span class="identifier">cend</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="comment">/* Derived Queries */</span>

    <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">name_for</span><span class="special">(</span><span class="identifier">customer_info</span><span class="special">::</span><span class="identifier">identifier</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">id</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">id_active</span><span class="special">(</span><span class="identifier">id</span><span class="special">));</span> <span class="comment">// Active.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="comment">// Find != end because of preconditions (no defensive programming).</span>
        <span class="keyword">return</span> <span class="identifier">customers_</span><span class="special">.</span><span class="identifier">find</span><span class="special">(</span><span class="identifier">id</span><span class="special">)-&gt;</span><span class="identifier">second</span><span class="special">.</span><span class="identifier">name_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">/* Commands */</span>

    <span class="keyword">void</span> <span class="identifier">add</span><span class="special">(</span><span class="identifier">customer_info</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">info</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">old_count</span> <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">count</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Not already active.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">id_active</span><span class="special">(</span><span class="identifier">info</span><span class="special">.</span><span class="identifier">id</span><span class="special">));</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_count</span> <span class="special">+</span> <span class="number">1</span><span class="special">);</span> <span class="comment">// Count inc.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">id_active</span><span class="special">(</span><span class="identifier">info</span><span class="special">.</span><span class="identifier">id</span><span class="special">));</span> <span class="comment">// Activated.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">customers_</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">make_pair</span><span class="special">(</span><span class="identifier">info</span><span class="special">.</span><span class="identifier">id</span><span class="special">,</span> <span class="identifier">customer</span><span class="special">(</span><span class="identifier">info</span><span class="special">)));</span>
    <span class="special">}</span>

    <span class="keyword">void</span> <span class="identifier">set_name</span><span class="special">(</span><span class="identifier">customer_info</span><span class="special">::</span><span class="identifier">identifier</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">id</span><span class="special">,</span>
            <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">name</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">id_active</span><span class="special">(</span><span class="identifier">id</span><span class="special">));</span> <span class="comment">// Already active.</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">name_for</span><span class="special">(</span><span class="identifier">id</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">name</span><span class="special">);</span> <span class="comment">// Name set.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="comment">// Find != end because of precondition (no defensive programming).</span>
        <span class="identifier">customers_</span><span class="special">.</span><span class="identifier">find</span><span class="special">(</span><span class="identifier">id</span><span class="special">)-&gt;</span><span class="identifier">second</span><span class="special">.</span><span class="identifier">name_</span> <span class="special">=</span> <span class="identifier">name</span><span class="special">;</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="keyword">class</span> <span class="identifier">agent</span> <span class="special">{};</span> <span class="comment">// Customer agent.</span>

    <span class="keyword">struct</span> <span class="identifier">customer</span> <span class="special">:</span> <span class="identifier">customer_info</span> <span class="special">{</span>
        <span class="identifier">agent</span> <span class="identifier">managing_agent</span><span class="special">;</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">last_contact</span><span class="special">;</span>

        <span class="keyword">explicit</span> <span class="identifier">customer</span><span class="special">(</span><span class="identifier">customer_info</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">info</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">customer_info</span><span class="special">(</span><span class="identifier">info</span><span class="special">),</span>
                <span class="identifier">managing_agent</span><span class="special">(),</span> <span class="identifier">last_contact</span><span class="special">()</span> <span class="special">{}</span>
    <span class="special">};</span>

    <span class="identifier">std</span><span class="special">::</span><span class="identifier">map</span><span class="special">&lt;</span><span class="identifier">customer_info</span><span class="special">::</span><span class="identifier">identifier</span><span class="special">,</span> <span class="identifier">customer</span><span class="special">&gt;</span> <span class="identifier">customers_</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">customer_manager</span> <span class="identifier">m</span><span class="special">;</span>
    <span class="identifier">customer_info</span> <span class="keyword">const</span> <span class="identifier">js</span><span class="special">(</span><span class="string">"john_smith_123"</span><span class="special">);</span>
    <span class="identifier">m</span><span class="special">.</span><span class="identifier">add</span><span class="special">(</span><span class="identifier">js</span><span class="special">);</span>
    <span class="identifier">m</span><span class="special">.</span><span class="identifier">set_name</span><span class="special">(</span><span class="identifier">js</span><span class="special">.</span><span class="identifier">id</span><span class="special">,</span> <span class="string">"John Smith"</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">m</span><span class="special">.</span><span class="identifier">name_for</span><span class="special">(</span><span class="identifier">js</span><span class="special">.</span><span class="identifier">id</span><span class="special">)</span> <span class="special">==</span> <span class="string">"John Smith"</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">m</span><span class="special">.</span><span class="identifier">count</span><span class="special">()</span> <span class="special">==</span> <span class="number">1</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">m</span><span class="special">.</span><span class="identifier">id_active</span><span class="special">(</span><span class="identifier">js</span><span class="special">.</span><span class="identifier">id</span><span class="special">));</span>
    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__mitchell02___observer__pure_virtual_functions"></a><a class="link" href="examples.html#boost_contract.examples.__mitchell02___observer__pure_virtual_functions" title="[Mitchell02] Observer: Pure virtual functions">[Mitchell02]
      Observer: Pure virtual functions</a>
</h3></div></div></div>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#ifndef</span> <span class="identifier">OBSERVER_HPP_</span>
<span class="preprocessor">#define</span> <span class="identifier">OBSERVER_HPP_</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="comment">// Observer.</span>
<span class="keyword">class</span> <span class="identifier">observer</span> <span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">subject</span><span class="special">;</span>
<span class="keyword">public</span><span class="special">:</span>
    <span class="comment">// No inv and no bases so contracts optional if no pre, post, and override.</span>

    <span class="comment">/* Creation */</span>

    <span class="identifier">observer</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Could have omitted contracts here (nothing to check).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">observer</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Could have omitted contracts here (nothing to check).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">/* Commands */</span>

    <span class="comment">// If up-to-date with related subject.</span>
    <span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="identifier">up_to_date_with_subject</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span>
            <span class="keyword">const</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>

    <span class="comment">// Update this observer.</span>
    <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">update</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">bool</span> <span class="identifier">observer</span><span class="special">::</span><span class="identifier">up_to_date_with_subject</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="keyword">this</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="keyword">false</span><span class="special">);</span>
    <span class="keyword">return</span> <span class="keyword">false</span><span class="special">;</span>
<span class="special">}</span>

<span class="keyword">void</span> <span class="identifier">observer</span><span class="special">::</span><span class="identifier">update</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span><span class="special">)</span> <span class="special">{</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="keyword">this</span><span class="special">)</span>
        <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">up_to_date_with_subject</span><span class="special">());</span> <span class="comment">// Up-to-date.</span>
        <span class="special">})</span>
    <span class="special">;</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="keyword">false</span><span class="special">);</span>
<span class="special">}</span>

<span class="preprocessor">#endif</span> <span class="comment">// #include guard</span>
</pre>
<p>
      </p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#ifndef</span> <span class="identifier">SUBJECT_HPP_</span>
<span class="preprocessor">#define</span> <span class="identifier">SUBJECT_HPP_</span>

<span class="preprocessor">#include</span> <span class="string">"observer.hpp"</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">algorithm</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="comment">// Subject for observer design pattern.</span>
<span class="keyword">class</span> <span class="identifier">subject</span> <span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">void</span> <span class="identifier">invariant</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT_AUDIT</span><span class="special">(</span><span class="identifier">all_observers_valid</span><span class="special">(</span><span class="identifier">observers</span><span class="special">()));</span> <span class="comment">// Valid.</span>
    <span class="special">}</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="comment">/* Creation */</span>

    <span class="comment">// Construct subject with no observer.</span>
    <span class="identifier">subject</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Check invariant.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">// Destroy subject.</span>
    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">subject</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Check invariant.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">/* Queries */</span>

    <span class="comment">// If given object is attached.</span>
    <span class="keyword">bool</span> <span class="identifier">attached</span><span class="special">(</span><span class="identifier">observer</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">ob</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">ob</span><span class="special">);</span> <span class="comment">// Not null.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">find</span><span class="special">(</span><span class="identifier">observers_</span><span class="special">.</span><span class="identifier">cbegin</span><span class="special">(),</span> <span class="identifier">observers_</span><span class="special">.</span><span class="identifier">cend</span><span class="special">(),</span> <span class="identifier">ob</span><span class="special">)</span> <span class="special">!=</span>
                <span class="identifier">observers_</span><span class="special">.</span><span class="identifier">cend</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="comment">/* Commands */</span>

    <span class="comment">// Attach given object as an observer.</span>
    <span class="keyword">void</span> <span class="identifier">attach</span><span class="special">(</span><span class="identifier">observer</span><span class="special">*</span> <span class="identifier">ob</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">observer</span> <span class="keyword">const</span><span class="special">*&gt;</span> <span class="special">&gt;</span> <span class="identifier">old_observers</span><span class="special">;</span>
        <span class="preprocessor">#ifdef</span> <span class="identifier">BOOST_CONTRACT_AUDITS</span>
            <span class="identifier">old_observers</span> <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">observers</span><span class="special">());</span>
        <span class="preprocessor">#endif</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">ob</span><span class="special">);</span> <span class="comment">// Not null.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">attached</span><span class="special">(</span><span class="identifier">ob</span><span class="special">));</span> <span class="comment">// Not already attached.</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">attached</span><span class="special">(</span><span class="identifier">ob</span><span class="special">));</span> <span class="comment">// Attached.</span>
                <span class="comment">// Others not changed (frame rule).</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT_AUDIT</span><span class="special">(</span><span class="identifier">other_observers_unchanged</span><span class="special">(</span>
                        <span class="special">*</span><span class="identifier">old_observers</span><span class="special">,</span> <span class="identifier">observers</span><span class="special">(),</span> <span class="identifier">ob</span><span class="special">));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">observers_</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">ob</span><span class="special">);</span>
    <span class="special">}</span>

<span class="keyword">protected</span><span class="special">:</span>
    <span class="comment">// Contracts could have been omitted for protected/private with no pre/post.</span>

    <span class="comment">/* Queries */</span>

    <span class="comment">// All observers attached to this subject.</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">observer</span> <span class="keyword">const</span><span class="special">*&gt;</span> <span class="identifier">observers</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">observer</span> <span class="keyword">const</span><span class="special">*&gt;</span> <span class="identifier">obs</span><span class="special">;</span>
        <span class="keyword">for</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">observer</span><span class="special">*&gt;::</span><span class="identifier">const_iterator</span> <span class="identifier">i</span> <span class="special">=</span> <span class="identifier">observers_</span><span class="special">.</span><span class="identifier">cbegin</span><span class="special">();</span>
                <span class="identifier">i</span> <span class="special">!=</span> <span class="identifier">observers_</span><span class="special">.</span><span class="identifier">cend</span><span class="special">();</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
            <span class="identifier">obs</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(*</span><span class="identifier">i</span><span class="special">);</span>
        <span class="special">}</span>
        <span class="keyword">return</span> <span class="identifier">obs</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">/* Commands */</span>

    <span class="comment">// Update all attached observers.</span>
    <span class="keyword">void</span> <span class="identifier">notify</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Protected members use `function` (no inv and no subcontracting).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">function</span><span class="special">()</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// All updated.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT_AUDIT</span><span class="special">(</span><span class="identifier">all_observers_updated</span><span class="special">(</span><span class="identifier">observers</span><span class="special">()));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">for</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">observer</span><span class="special">*&gt;::</span><span class="identifier">iterator</span> <span class="identifier">i</span> <span class="special">=</span> <span class="identifier">observers_</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
                <span class="identifier">i</span> <span class="special">!=</span> <span class="identifier">observers_</span><span class="special">.</span><span class="identifier">end</span><span class="special">();</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
            <span class="comment">// Class invariants ensure no null pointers in observers but class</span>
            <span class="comment">// invariants not checked for non-public functions so assert here.</span>
            <span class="identifier">assert</span><span class="special">(*</span><span class="identifier">i</span><span class="special">);</span> <span class="comment">// Pointer not null (defensive programming).</span>
            <span class="special">(*</span><span class="identifier">i</span><span class="special">)-&gt;</span><span class="identifier">update</span><span class="special">();</span>
        <span class="special">}</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="comment">/* Contract Helpers */</span>

    <span class="keyword">static</span> <span class="keyword">bool</span> <span class="identifier">all_observers_valid</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">observer</span> <span class="keyword">const</span><span class="special">*&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">obs</span><span class="special">)</span> <span class="special">{</span>
        <span class="keyword">for</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">observer</span> <span class="keyword">const</span><span class="special">*&gt;::</span><span class="identifier">const_iterator</span> <span class="identifier">i</span> <span class="special">=</span> <span class="identifier">obs</span><span class="special">.</span><span class="identifier">cbegin</span><span class="special">();</span>
                <span class="identifier">i</span> <span class="special">!=</span> <span class="identifier">obs</span><span class="special">.</span><span class="identifier">cend</span><span class="special">();</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
            <span class="keyword">if</span><span class="special">(!*</span><span class="identifier">i</span><span class="special">)</span> <span class="keyword">return</span> <span class="keyword">false</span><span class="special">;</span>
        <span class="special">}</span>
        <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">static</span> <span class="keyword">bool</span> <span class="identifier">other_observers_unchanged</span><span class="special">(</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">observer</span> <span class="keyword">const</span><span class="special">*&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">old_obs</span><span class="special">,</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">observer</span> <span class="keyword">const</span><span class="special">*&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">new_obs</span><span class="special">,</span>
        <span class="identifier">observer</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">ob</span>
    <span class="special">)</span> <span class="special">{</span>
        <span class="comment">// Private members use `function` (no inv and no subcontracting).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">function</span><span class="special">()</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">ob</span><span class="special">);</span> <span class="comment">// Not null.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">observer</span> <span class="keyword">const</span><span class="special">*&gt;</span> <span class="identifier">remaining</span> <span class="special">=</span> <span class="identifier">new_obs</span><span class="special">;</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="identifier">remaining</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">remaining</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">ob</span><span class="special">);</span>

        <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">observer</span> <span class="keyword">const</span><span class="special">*&gt;::</span><span class="identifier">const_iterator</span> <span class="identifier">remaining_it</span> <span class="special">=</span>
                <span class="identifier">remaining</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">observer</span> <span class="keyword">const</span><span class="special">*&gt;::</span><span class="identifier">const_iterator</span> <span class="identifier">old_it</span> <span class="special">=</span> <span class="identifier">old_obs</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
        <span class="keyword">while</span><span class="special">(</span><span class="identifier">remaining</span><span class="special">.</span><span class="identifier">cend</span><span class="special">()</span> <span class="special">!=</span> <span class="identifier">remaining_it</span> <span class="special">&amp;&amp;</span> <span class="identifier">old_obs</span><span class="special">.</span><span class="identifier">cend</span><span class="special">()</span> <span class="special">!=</span> <span class="identifier">old_it</span><span class="special">)</span> <span class="special">{</span>
            <span class="keyword">if</span><span class="special">(*</span><span class="identifier">remaining_it</span> <span class="special">!=</span> <span class="special">*</span><span class="identifier">old_it</span><span class="special">)</span> <span class="keyword">return</span> <span class="keyword">false</span><span class="special">;</span>
            <span class="special">++</span><span class="identifier">remaining_it</span><span class="special">;</span>
            <span class="special">++</span><span class="identifier">old_it</span><span class="special">;</span>
        <span class="special">}</span>
        <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">static</span> <span class="keyword">bool</span> <span class="identifier">all_observers_updated</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">observer</span> <span class="keyword">const</span><span class="special">*&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">obs</span><span class="special">)</span> <span class="special">{</span>
        <span class="keyword">for</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">observer</span> <span class="keyword">const</span><span class="special">*&gt;::</span><span class="identifier">const_iterator</span> <span class="identifier">i</span> <span class="special">=</span> <span class="identifier">obs</span><span class="special">.</span><span class="identifier">cbegin</span><span class="special">();</span>
                <span class="identifier">i</span> <span class="special">!=</span> <span class="identifier">obs</span><span class="special">.</span><span class="identifier">cend</span><span class="special">();</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
            <span class="keyword">if</span><span class="special">(!*</span><span class="identifier">i</span><span class="special">)</span> <span class="keyword">return</span> <span class="keyword">false</span><span class="special">;</span>
            <span class="keyword">if</span><span class="special">(!(*</span><span class="identifier">i</span><span class="special">)-&gt;</span><span class="identifier">up_to_date_with_subject</span><span class="special">())</span> <span class="keyword">return</span> <span class="keyword">false</span><span class="special">;</span>
        <span class="special">}</span>
        <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">observer</span><span class="special">*&gt;</span> <span class="identifier">observers_</span><span class="special">;</span>
<span class="special">};</span>

<span class="preprocessor">#endif</span> <span class="comment">// #include guard</span>
</pre>
<p>
      </p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="string">"observer/observer.hpp"</span>
<span class="preprocessor">#include</span> <span class="string">"observer/subject.hpp"</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="keyword">int</span> <span class="identifier">test_state</span><span class="special">;</span> <span class="comment">// For testing only.</span>

<span class="comment">// Implement an actual subject.</span>
<span class="keyword">class</span> <span class="identifier">concrete_subject</span>
    <span class="preprocessor">#define</span> <span class="identifier">BASES</span> <span class="keyword">public</span> <span class="identifier">subject</span>
    <span class="special">:</span> <span class="identifier">BASES</span>
<span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">typedef</span> <span class="identifier">BOOST_CONTRACT_BASE_TYPES</span><span class="special">(</span><span class="identifier">BASES</span><span class="special">)</span> <span class="identifier">base_types</span><span class="special">;</span> <span class="comment">// Subcontracting.</span>
    <span class="preprocessor">#undef</span> <span class="identifier">BASES</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="keyword">typedef</span> <span class="keyword">int</span> <span class="identifier">state</span><span class="special">;</span> <span class="comment">// Some state being observed.</span>

    <span class="identifier">concrete_subject</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">state_</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Could have omitted contracts here (nothing to check).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">concrete_subject</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Could have omitted contracts here (nothing to check).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">void</span> <span class="identifier">set_state</span><span class="special">(</span><span class="identifier">state</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">new_state</span><span class="special">)</span> <span class="special">{</span>
        <span class="comment">// Could have omitted contracts here (nothing to check).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>

        <span class="identifier">state_</span> <span class="special">=</span> <span class="identifier">new_state</span><span class="special">;</span>
        <span class="identifier">assert</span><span class="special">(</span><span class="identifier">state_</span> <span class="special">==</span> <span class="identifier">test_state</span><span class="special">);</span>
        <span class="identifier">notify</span><span class="special">();</span> <span class="comment">// Notify all observers.</span>
    <span class="special">}</span>

    <span class="identifier">state</span> <span class="identifier">get_state</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Could have omitted contracts here (nothing to check).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">state_</span><span class="special">;</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="identifier">state</span> <span class="identifier">state_</span><span class="special">;</span>
<span class="special">};</span>

<span class="comment">// Implement an actual observer.</span>
<span class="keyword">class</span> <span class="identifier">concrete_observer</span>
    <span class="preprocessor">#define</span> <span class="identifier">BASES</span> <span class="keyword">public</span> <span class="identifier">observer</span>
    <span class="special">:</span> <span class="identifier">BASES</span>
<span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">typedef</span> <span class="identifier">BOOST_CONTRACT_BASE_TYPES</span><span class="special">(</span><span class="identifier">BASES</span><span class="special">)</span> <span class="identifier">base_types</span><span class="special">;</span> <span class="comment">// Subcontracting.</span>
    <span class="preprocessor">#undef</span> <span class="identifier">BASES</span>

    <span class="identifier">BOOST_CONTRACT_OVERRIDES</span><span class="special">(</span><span class="identifier">up_to_date_with_subject</span><span class="special">,</span> <span class="identifier">update</span><span class="special">)</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="comment">// Create concrete observer.</span>
    <span class="keyword">explicit</span> <span class="identifier">concrete_observer</span><span class="special">(</span><span class="identifier">concrete_subject</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">subj</span><span class="special">)</span> <span class="special">:</span>
            <span class="identifier">subject_</span><span class="special">(</span><span class="identifier">subj</span><span class="special">),</span> <span class="identifier">observed_state_</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Could have omitted contracts here (nothing to check).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">concrete_observer</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Could have omitted contracts here (nothing to check).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">// Implement base virtual functions.</span>

    <span class="keyword">bool</span> <span class="identifier">up_to_date_with_subject</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span>
            <span class="keyword">const</span> <span class="comment">/* override */</span> <span class="special">{</span>
        <span class="keyword">bool</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">&lt;</span>
            <span class="identifier">override_up_to_date_with_subject</span>
        <span class="special">&gt;(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">result</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">concrete_observer</span><span class="special">::</span><span class="identifier">up_to_date_with_subject</span><span class="special">,</span> <span class="keyword">this</span><span class="special">);</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">;</span> <span class="comment">// For simplicity, assume always up-to-date.</span>
    <span class="special">}</span>

    <span class="keyword">void</span> <span class="identifier">update</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="comment">/* override */</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">&lt;</span>
                <span class="identifier">override_update</span><span class="special">&gt;(</span><span class="identifier">v</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">concrete_observer</span><span class="special">::</span><span class="identifier">update</span><span class="special">,</span> <span class="keyword">this</span><span class="special">);</span>

        <span class="identifier">observed_state_</span> <span class="special">=</span> <span class="identifier">subject_</span><span class="special">.</span><span class="identifier">get_state</span><span class="special">();</span>
        <span class="identifier">assert</span><span class="special">(</span><span class="identifier">observed_state_</span> <span class="special">==</span> <span class="identifier">test_state</span><span class="special">);</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="identifier">concrete_subject</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">subject_</span><span class="special">;</span>
    <span class="identifier">concrete_subject</span><span class="special">::</span><span class="identifier">state</span> <span class="identifier">observed_state_</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">concrete_subject</span> <span class="identifier">subj</span><span class="special">;</span>
    <span class="identifier">concrete_observer</span> <span class="identifier">ob</span><span class="special">(</span><span class="identifier">subj</span><span class="special">);</span>
    <span class="identifier">subj</span><span class="special">.</span><span class="identifier">attach</span><span class="special">(&amp;</span><span class="identifier">ob</span><span class="special">);</span>

    <span class="identifier">subj</span><span class="special">.</span><span class="identifier">set_state</span><span class="special">(</span><span class="identifier">test_state</span> <span class="special">=</span> <span class="number">123</span><span class="special">);</span>
    <span class="identifier">subj</span><span class="special">.</span><span class="identifier">set_state</span><span class="special">(</span><span class="identifier">test_state</span> <span class="special">=</span> <span class="number">456</span><span class="special">);</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__mitchell02___counter__subcontracting"></a><a name="Mitchell02_counter_anchor"></a><a class="link" href="examples.html#boost_contract.examples.__mitchell02___counter__subcontracting" title="[Mitchell02] Counter: Subcontracting">[Mitchell02]
      Counter: Subcontracting</a>
</h3></div></div></div>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#ifndef</span> <span class="identifier">PUSH_BUTTON_HPP_</span>
<span class="preprocessor">#define</span> <span class="identifier">PUSH_BUTTON_HPP_</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="keyword">class</span> <span class="identifier">push_button</span> <span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
    <span class="comment">// No inv and no bases so contracts optional if no pre, post, and override.</span>

    <span class="comment">/* Creation */</span>

    <span class="comment">// Create an enabled button.</span>
    <span class="identifier">push_button</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">enabled_</span><span class="special">(</span><span class="keyword">true</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">enabled</span><span class="special">());</span> <span class="comment">// Enabled.</span>
            <span class="special">})</span>
        <span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">// Destroy button.</span>
    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">push_button</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Could have omitted contracts here (nothing to check).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">/* Queries */</span>

    <span class="comment">// If button is enabled.</span>
    <span class="keyword">bool</span> <span class="identifier">enabled</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Could have omitted contracts here (nothing to check).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">enabled_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">/* Commands */</span>

    <span class="comment">// Enable button.</span>
    <span class="keyword">void</span> <span class="identifier">enable</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">enabled</span><span class="special">());</span> <span class="comment">// Enabled.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">enabled_</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">// Disable button.</span>
    <span class="keyword">void</span> <span class="identifier">disable</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">enabled</span><span class="special">());</span> <span class="comment">// Disabled.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">enabled_</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">// Invoke externally when button clicked.</span>
    <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">on_bn_clicked</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="keyword">bool</span> <span class="identifier">enabled_</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">void</span> <span class="identifier">push_button</span><span class="special">::</span><span class="identifier">on_bn_clicked</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span><span class="special">)</span> <span class="special">{</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="keyword">this</span><span class="special">)</span>
        <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">enabled</span><span class="special">());</span> <span class="comment">// Enabled.</span>
        <span class="special">})</span>
    <span class="special">;</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="keyword">false</span><span class="special">);</span>
<span class="special">}</span>

<span class="preprocessor">#endif</span> <span class="comment">// #include guard</span>
</pre>
<p>
      </p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#ifndef</span> <span class="identifier">DECREMENT_BUTTON_HPP_</span>
<span class="preprocessor">#define</span> <span class="identifier">DECREMENT_BUTTON_HPP_</span>

<span class="preprocessor">#include</span> <span class="string">"push_button.hpp"</span>
<span class="preprocessor">#include</span> <span class="string">"counter.hpp"</span>
<span class="preprocessor">#include</span> <span class="string">"../observer/observer.hpp"</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">noncopyable</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">class</span> <span class="identifier">decrement_button</span>
    <span class="preprocessor">#define</span> <span class="identifier">BASES</span> <span class="keyword">public</span> <span class="identifier">push_button</span><span class="special">,</span> <span class="keyword">public</span> <span class="identifier">observer</span><span class="special">,</span> <span class="special">\</span>
            <span class="keyword">private</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">noncopyable</span>
    <span class="special">:</span> <span class="identifier">BASES</span>
<span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">typedef</span> <span class="identifier">BOOST_CONTRACT_BASE_TYPES</span><span class="special">(</span><span class="identifier">BASES</span><span class="special">)</span> <span class="identifier">base_types</span><span class="special">;</span>
    <span class="preprocessor">#undef</span> <span class="identifier">BASES</span>

    <span class="identifier">BOOST_CONTRACT_OVERRIDES</span><span class="special">(</span><span class="identifier">on_bn_clicked</span><span class="special">,</span> <span class="identifier">up_to_date_with_subject</span><span class="special">,</span> <span class="identifier">update</span><span class="special">);</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="comment">/* Creation */</span>

    <span class="keyword">explicit</span> <span class="identifier">decrement_button</span><span class="special">(</span><span class="identifier">counter</span><span class="special">&amp;</span> <span class="identifier">a_counter</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">counter_</span><span class="special">(</span><span class="identifier">a_counter</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Enable iff positive value.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">enabled</span><span class="special">()</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">a_counter</span><span class="special">.</span><span class="identifier">value</span><span class="special">()</span> <span class="special">&gt;</span> <span class="number">0</span><span class="special">));</span>
            <span class="special">})</span>
        <span class="special">;</span>
        <span class="identifier">counter_</span><span class="special">.</span><span class="identifier">attach</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">// Destroy button.</span>
    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">decrement_button</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Could have omitted contracts here (nothing to check).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">/* Commands */</span>

    <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">on_bn_clicked</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span>
            <span class="comment">/* override */</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">old_value</span> <span class="special">=</span>
                <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">counter_</span><span class="special">.</span><span class="identifier">value</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">&lt;</span>
            <span class="identifier">override_on_bn_clicked</span>
        <span class="special">&gt;(</span><span class="identifier">v</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">decrement_button</span><span class="special">::</span><span class="identifier">on_bn_clicked</span><span class="special">,</span> <span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Counter decremented.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">counter_</span><span class="special">.</span><span class="identifier">value</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_value</span> <span class="special">-</span> <span class="number">1</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>
        <span class="identifier">counter_</span><span class="special">.</span><span class="identifier">decrement</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="identifier">up_to_date_with_subject</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span>
            <span class="keyword">const</span> <span class="comment">/* override */</span> <span class="special">{</span>
        <span class="keyword">bool</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">&lt;</span>
            <span class="identifier">override_up_to_date_with_subject</span>
        <span class="special">&gt;(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">result</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">decrement_button</span><span class="special">::</span><span class="identifier">up_to_date_with_subject</span><span class="special">,</span> <span class="keyword">this</span><span class="special">);</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">;</span> <span class="comment">// For simplicity, assume always up-to-date.</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">update</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="comment">/* override */</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">&lt;</span>
                <span class="identifier">override_update</span><span class="special">&gt;(</span><span class="identifier">v</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">decrement_button</span><span class="special">::</span><span class="identifier">update</span><span class="special">,</span> <span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// Enabled iff positive value.</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">enabled</span><span class="special">()</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">counter_</span><span class="special">.</span><span class="identifier">value</span><span class="special">()</span> <span class="special">&gt;</span> <span class="number">0</span><span class="special">));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">if</span><span class="special">(</span><span class="identifier">counter_</span><span class="special">.</span><span class="identifier">value</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">)</span> <span class="identifier">disable</span><span class="special">();</span>
        <span class="keyword">else</span> <span class="identifier">enable</span><span class="special">();</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="identifier">counter</span><span class="special">&amp;</span> <span class="identifier">counter_</span><span class="special">;</span>
<span class="special">};</span>

<span class="preprocessor">#endif</span> <span class="comment">// #include guard</span>
</pre>
<p>
      </p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#ifndef</span> <span class="identifier">COUNTER_HPP_</span>
<span class="preprocessor">#define</span> <span class="identifier">COUNTER_HPP_</span>

<span class="preprocessor">#include</span> <span class="string">"../observer/subject.hpp"</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">class</span> <span class="identifier">counter</span>
    <span class="preprocessor">#define</span> <span class="identifier">BASES</span> <span class="keyword">public</span> <span class="identifier">subject</span>
    <span class="special">:</span> <span class="identifier">BASES</span>
<span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">typedef</span> <span class="identifier">BOOST_CONTRACT_BASE_TYPES</span><span class="special">(</span><span class="identifier">BASES</span><span class="special">)</span> <span class="identifier">base_types</span><span class="special">;</span>
    <span class="preprocessor">#undef</span> <span class="identifier">BASES</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="comment">/* Creation */</span>

    <span class="comment">// Construct counter with specified value.</span>
    <span class="keyword">explicit</span> <span class="identifier">counter</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">a_value</span> <span class="special">=</span> <span class="number">10</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">value_</span><span class="special">(</span><span class="identifier">a_value</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">value</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">a_value</span><span class="special">);</span> <span class="comment">// Value set.</span>
            <span class="special">})</span>
        <span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">// Destroy counter.</span>
    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">counter</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Could have omitted contracts here (nothing to check).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">/* Queries */</span>

    <span class="comment">// Current counter value.</span>
    <span class="keyword">int</span> <span class="identifier">value</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Could have omitted contracts here (nothing to check).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">value_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="comment">/* Commands */</span>

    <span class="comment">// Decrement counter value.</span>
    <span class="keyword">void</span> <span class="identifier">decrement</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">old_value</span> <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">value</span><span class="special">());</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">value</span><span class="special">()</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_value</span> <span class="special">-</span> <span class="number">1</span><span class="special">);</span> <span class="comment">// Decrement.</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="special">--</span><span class="identifier">value_</span><span class="special">;</span>
        <span class="identifier">notify</span><span class="special">();</span> <span class="comment">// Notify all attached observers.</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="keyword">int</span> <span class="identifier">value_</span><span class="special">;</span>
<span class="special">};</span>

<span class="preprocessor">#endif</span> <span class="comment">// #include guard</span>
</pre>
<p>
      </p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="string">"counter/counter.hpp"</span>
<span class="preprocessor">#include</span> <span class="string">"counter/decrement_button.hpp"</span>
<span class="preprocessor">#include</span> <span class="string">"observer/observer.hpp"</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="keyword">int</span> <span class="identifier">test_counter</span><span class="special">;</span>

<span class="keyword">class</span> <span class="identifier">view_of_counter</span>
    <span class="preprocessor">#define</span> <span class="identifier">BASES</span> <span class="keyword">public</span> <span class="identifier">observer</span>
    <span class="special">:</span> <span class="identifier">BASES</span>
<span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">typedef</span> <span class="identifier">BOOST_CONTRACT_BASE_TYPES</span><span class="special">(</span><span class="identifier">BASES</span><span class="special">)</span> <span class="identifier">base_types</span><span class="special">;</span>
    <span class="preprocessor">#undef</span> <span class="identifier">BASES</span>

    <span class="identifier">BOOST_CONTRACT_OVERRIDES</span><span class="special">(</span><span class="identifier">up_to_date_with_subject</span><span class="special">,</span> <span class="identifier">update</span><span class="special">)</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="comment">/* Creation */</span>

    <span class="comment">// Create view associated with given counter.</span>
    <span class="keyword">explicit</span> <span class="identifier">view_of_counter</span><span class="special">(</span><span class="identifier">counter</span><span class="special">&amp;</span> <span class="identifier">a_counter</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">counter_</span><span class="special">(</span><span class="identifier">a_counter</span><span class="special">)</span> <span class="special">{</span>
        <span class="comment">// Could have omitted contracts here (nothing to check).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>

        <span class="identifier">counter_</span><span class="special">.</span><span class="identifier">attach</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="identifier">assert</span><span class="special">(</span><span class="identifier">counter_</span><span class="special">.</span><span class="identifier">value</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">test_counter</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">// Destroy view.</span>
    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">view_of_counter</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Could have omitted contracts here (nothing to check).</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">/* Commands */</span>

    <span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="identifier">up_to_date_with_subject</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span>
            <span class="keyword">const</span> <span class="comment">/* override */</span> <span class="special">{</span>
        <span class="keyword">bool</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">&lt;</span>
            <span class="identifier">override_up_to_date_with_subject</span>
        <span class="special">&gt;(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">result</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">view_of_counter</span><span class="special">::</span><span class="identifier">up_to_date_with_subject</span><span class="special">,</span> <span class="keyword">this</span><span class="special">);</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">;</span> <span class="comment">// For simplicity, assume always up-to-date.</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">update</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="comment">/* override */</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">&lt;</span>
                <span class="identifier">override_update</span><span class="special">&gt;(</span><span class="identifier">v</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">view_of_counter</span><span class="special">::</span><span class="identifier">update</span><span class="special">,</span> <span class="keyword">this</span><span class="special">);</span>

        <span class="identifier">assert</span><span class="special">(</span><span class="identifier">counter_</span><span class="special">.</span><span class="identifier">value</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">test_counter</span><span class="special">);</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="identifier">counter</span><span class="special">&amp;</span> <span class="identifier">counter_</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">counter</span> <span class="identifier">cnt</span><span class="special">(</span><span class="identifier">test_counter</span> <span class="special">=</span> <span class="number">1</span><span class="special">);</span>
    <span class="identifier">view_of_counter</span> <span class="identifier">view</span><span class="special">(</span><span class="identifier">cnt</span><span class="special">);</span>

    <span class="identifier">decrement_button</span> <span class="identifier">dec</span><span class="special">(</span><span class="identifier">cnt</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">dec</span><span class="special">.</span><span class="identifier">enabled</span><span class="special">());</span>

    <span class="identifier">test_counter</span><span class="special">--;</span>
    <span class="identifier">dec</span><span class="special">.</span><span class="identifier">on_bn_clicked</span><span class="special">();</span>
    <span class="identifier">assert</span><span class="special">(!</span><span class="identifier">dec</span><span class="special">.</span><span class="identifier">enabled</span><span class="special">());</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__cline90___vector__comparison_with_a___proposal_syntax"></a><a name="Cline90_vector_anchor"></a><a class="link" href="examples.html#boost_contract.examples.__cline90___vector__comparison_with_a___proposal_syntax" title="[Cline90] Vector: Comparison with A++ proposal syntax">[Cline90]
      Vector: Comparison with A++ proposal syntax</a>
</h3></div></div></div>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  This Library
                </p>
              </th>
<th>
                <p>
                  A++ Proposal (never actually implemented)
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
                <p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#ifndef</span> <span class="identifier">VECTOR_HPP_</span>
<span class="preprocessor">#define</span> <span class="identifier">VECTOR_HPP_</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="comment">// NOTE: Incomplete contract assertions, addressing only `size`.</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">vector</span>
    <span class="preprocessor">#define</span> <span class="identifier">BASES</span> <span class="keyword">private</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor_precondition</span><span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;</span>
    <span class="special">:</span> <span class="identifier">BASES</span>
<span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">typedef</span> <span class="identifier">BOOST_CONTRACT_BASE_TYPES</span><span class="special">(</span><span class="identifier">BASES</span><span class="special">)</span> <span class="identifier">base_types</span><span class="special">;</span>
    <span class="preprocessor">#undef</span> <span class="identifier">BASES</span>

    <span class="keyword">void</span> <span class="identifier">invariant</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span>
    <span class="special">}</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="keyword">explicit</span> <span class="identifier">vector</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">count</span> <span class="special">=</span> <span class="number">10</span><span class="special">)</span> <span class="special">:</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor_precondition</span><span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&gt;([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span>
        <span class="special">}),</span>
        <span class="identifier">data_</span><span class="special">(</span><span class="keyword">new</span> <span class="identifier">T</span><span class="special">[</span><span class="identifier">count</span><span class="special">]),</span>
        <span class="identifier">size_</span><span class="special">(</span><span class="identifier">count</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">count</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">size_</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="identifier">data_</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">T</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">vector</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">delete</span><span class="special">[]</span> <span class="identifier">data_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">int</span> <span class="identifier">size</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">size_</span><span class="special">;</span> <span class="comment">// Non-negative result already checked by invariant.</span>
    <span class="special">}</span>

    <span class="keyword">void</span> <span class="identifier">resize</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">count</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">count</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">T</span><span class="special">*</span> <span class="identifier">slice</span> <span class="special">=</span> <span class="keyword">new</span> <span class="identifier">T</span><span class="special">[</span><span class="identifier">count</span><span class="special">];</span>
        <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">count</span> <span class="special">&amp;&amp;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">size_</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="identifier">slice</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">data_</span><span class="special">[</span><span class="identifier">i</span><span class="special">];</span>
        <span class="keyword">delete</span><span class="special">[]</span> <span class="identifier">data_</span><span class="special">;</span>
        <span class="identifier">data_</span> <span class="special">=</span> <span class="identifier">slice</span><span class="special">;</span>
        <span class="identifier">size_</span> <span class="special">=</span> <span class="identifier">count</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="identifier">T</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">[](</span><span class="keyword">int</span> <span class="identifier">index</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">index</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">index</span> <span class="special">&lt;</span> <span class="identifier">size</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">data_</span><span class="special">[</span><span class="identifier">index</span><span class="special">];</span>
    <span class="special">}</span>

    <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">[](</span><span class="keyword">int</span> <span class="identifier">index</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">index</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">index</span> <span class="special">&lt;</span> <span class="identifier">size</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">data_</span><span class="special">[</span><span class="identifier">index</span><span class="special">];</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="identifier">T</span><span class="special">*</span> <span class="identifier">data_</span><span class="special">;</span>
    <span class="keyword">int</span> <span class="identifier">size_</span><span class="special">;</span>
<span class="special">};</span>

<span class="preprocessor">#endif</span> <span class="comment">// #include guard</span>
</pre>
<p>
                </p>
              </td>
<td>
                <p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="comment">// Extra spaces, newlines, etc. for visual alignment with this library code.</span>





<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">vector</span> <span class="special">{</span>








<span class="identifier">legal</span><span class="special">:</span> <span class="comment">// Class invariants (legal).</span>
    <span class="identifier">size</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">;</span>


<span class="keyword">public</span><span class="special">:</span>
    <span class="keyword">explicit</span> <span class="identifier">vector</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">count</span> <span class="special">=</span> <span class="number">10</span><span class="special">)</span> <span class="special">:</span>
        <span class="identifier">data_</span><span class="special">(</span><span class="keyword">new</span> <span class="identifier">T</span><span class="special">[</span><span class="identifier">count</span><span class="special">]),</span>
        <span class="identifier">size_</span><span class="special">(</span><span class="identifier">count</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">size_</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="identifier">data_</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">T</span><span class="special">();</span>
    <span class="special">}</span>










    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">vector</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">delete</span><span class="special">[]</span> <span class="identifier">data_</span><span class="special">;</span> <span class="special">}</span>




    <span class="keyword">int</span> <span class="identifier">size</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">size_</span><span class="special">;</span> <span class="special">}</span>




    <span class="keyword">void</span> <span class="identifier">resize</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">count</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">T</span><span class="special">*</span> <span class="identifier">slice</span> <span class="special">=</span> <span class="keyword">new</span> <span class="identifier">T</span><span class="special">[</span><span class="identifier">count</span><span class="special">];</span>
        <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">count</span> <span class="special">&amp;&amp;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">size_</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="identifier">slice</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">data_</span><span class="special">[</span><span class="identifier">i</span><span class="special">];</span>
        <span class="keyword">delete</span><span class="special">[]</span> <span class="identifier">data_</span><span class="special">;</span>
        <span class="identifier">data_</span> <span class="special">=</span> <span class="identifier">slice</span><span class="special">;</span>
        <span class="identifier">size_</span> <span class="special">=</span> <span class="identifier">count</span><span class="special">;</span>
    <span class="special">}</span>










    <span class="identifier">T</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">[](</span><span class="keyword">int</span> <span class="identifier">index</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">data_</span><span class="special">[</span><span class="identifier">index</span><span class="special">];</span> <span class="special">}</span>










    <span class="identifier">T</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">[](</span><span class="keyword">int</span> <span class="identifier">index</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">data_</span><span class="special">[</span><span class="identifier">index</span><span class="special">];</span> <span class="special">}</span>




<span class="identifier">axioms</span><span class="special">:</span> <span class="comment">// Preconditions (require) and postconditions (promise) for each func.</span>
    <span class="special">[</span><span class="keyword">int</span> <span class="identifier">count</span><span class="special">;</span> <span class="identifier">require</span> <span class="identifier">count</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">promise</span> <span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">count</span><span class="special">]</span> <span class="identifier">vector</span><span class="special">(</span><span class="identifier">count</span><span class="special">);</span>
    <span class="special">[</span><span class="keyword">int</span> <span class="identifier">count</span><span class="special">;</span> <span class="identifier">require</span> <span class="identifier">count</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">promise</span> <span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">count</span><span class="special">]</span> <span class="identifier">resize</span><span class="special">(</span><span class="identifier">count</span><span class="special">);</span>
    <span class="special">[</span><span class="keyword">int</span> <span class="identifier">index</span><span class="special">;</span> <span class="identifier">require</span> <span class="identifier">index</span> <span class="special">&gt;=</span> <span class="number">0</span> <span class="special">&amp;&amp;</span> <span class="identifier">index</span> <span class="special">&lt;</span> <span class="identifier">size</span><span class="special">()]</span> <span class="special">(*</span><span class="keyword">this</span><span class="special">)[</span><span class="identifier">x</span><span class="special">];</span>       <span class="comment">// Op[].</span>
    <span class="special">[</span><span class="keyword">int</span> <span class="identifier">index</span><span class="special">;</span> <span class="identifier">require</span> <span class="identifier">index</span> <span class="special">&gt;=</span> <span class="number">0</span> <span class="special">&amp;&amp;</span> <span class="identifier">index</span> <span class="special">&lt;</span> <span class="identifier">size</span><span class="special">()]</span> <span class="special">(*</span><span class="keyword">this</span><span class="special">)[</span><span class="identifier">x</span><span class="special">]</span> <span class="keyword">const</span><span class="special">;</span> <span class="comment">// Op[].</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="identifier">T</span><span class="special">*</span> <span class="identifier">data_</span><span class="special">;</span>
    <span class="keyword">int</span> <span class="identifier">size_</span><span class="special">;</span>
<span class="special">};</span>

<span class="comment">// End.</span>
</pre>
<p>
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#include</span> <span class="string">"vector.hpp"</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">v</span> <span class="special">(</span><span class="number">3</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">3</span><span class="special">);</span>

    <span class="identifier">v</span><span class="special">[</span><span class="number">0</span><span class="special">]</span> <span class="special">=</span> <span class="number">123</span><span class="special">;</span>
    <span class="identifier">v</span><span class="special">.</span><span class="identifier">resize</span><span class="special">(</span><span class="number">2</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">v</span><span class="special">[</span><span class="number">0</span><span class="special">]</span> <span class="special">==</span> <span class="number">123</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">2</span><span class="special">);</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
                </p>
              </td>
<td>
              </td>
</tr>
</tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__cline90___stack__stack_like_container"></a><a class="link" href="examples.html#boost_contract.examples.__cline90___stack__stack_like_container" title="[Cline90] Stack: Stack-like container">[Cline90]
      Stack: Stack-like container</a>
</h3></div></div></div>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="comment">// NOTE: Incomplete contract assertions, addressing only `empty` and `full`.</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">stack</span>
    <span class="preprocessor">#define</span> <span class="identifier">BASES</span> <span class="keyword">private</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor_precondition</span><span class="special">&lt;</span><span class="identifier">stack</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;</span>
    <span class="special">:</span> <span class="identifier">BASES</span>
<span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">typedef</span> <span class="identifier">BOOST_CONTRACT_BASE_TYPES</span><span class="special">(</span><span class="identifier">BASES</span><span class="special">)</span> <span class="identifier">base_types</span><span class="special">;</span>
    <span class="preprocessor">#undef</span> <span class="identifier">BASES</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="keyword">explicit</span> <span class="identifier">stack</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">capacity</span><span class="special">)</span> <span class="special">:</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor_precondition</span><span class="special">&lt;</span><span class="identifier">stack</span><span class="special">&gt;([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">capacity</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span>
        <span class="special">}),</span>
        <span class="identifier">data_</span><span class="special">(</span><span class="keyword">new</span> <span class="identifier">T</span><span class="special">[</span><span class="identifier">capacity</span><span class="special">]),</span> <span class="identifier">capacity_</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">),</span> <span class="identifier">size_</span><span class="special">(</span><span class="number">0</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">empty</span><span class="special">());</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">full</span><span class="special">()</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">capacity</span> <span class="special">==</span> <span class="number">0</span><span class="special">));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">capacity_</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="identifier">data_</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">T</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">stack</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">delete</span><span class="special">[]</span> <span class="identifier">data_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">bool</span> <span class="identifier">empty</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">size_</span> <span class="special">==</span> <span class="number">0</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">bool</span> <span class="identifier">full</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">size_</span> <span class="special">==</span> <span class="identifier">capacity_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">void</span> <span class="identifier">push</span><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">full</span><span class="special">());</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">empty</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">data_</span><span class="special">[</span><span class="identifier">size_</span><span class="special">++]</span> <span class="special">=</span> <span class="identifier">value</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="identifier">T</span> <span class="identifier">pop</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">empty</span><span class="special">());</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">full</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">data_</span><span class="special">[--</span><span class="identifier">size_</span><span class="special">];</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="identifier">T</span><span class="special">*</span> <span class="identifier">data_</span><span class="special">;</span>
    <span class="keyword">int</span> <span class="identifier">capacity_</span><span class="special">;</span>
    <span class="keyword">int</span> <span class="identifier">size_</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">stack</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">s</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
    <span class="identifier">s</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">123</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">pop</span><span class="special">()</span> <span class="special">==</span> <span class="number">123</span><span class="special">);</span>
    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__cline90___vector_stack__subcontracting"></a><a class="link" href="examples.html#boost_contract.examples.__cline90___vector_stack__subcontracting" title="[Cline90] Vector-Stack: Subcontracting">[Cline90]
      Vector-Stack: Subcontracting</a>
</h3></div></div></div>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="string">"vector.hpp"</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">optional</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="comment">// NOTE: Incomplete contract assertions, addressing only `empty` and `full`.</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">abstract_stack</span> <span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
    <span class="identifier">abstract_stack</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="comment">// AXIOM as empty() cannot actually be checked here to avoid</span>
                <span class="comment">// calling pure virtual function length() during construction).</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT_AXIOM</span><span class="special">(</span><span class="identifier">empty</span><span class="special">());</span>
            <span class="special">})</span>
        <span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">abstract_stack</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">bool</span> <span class="identifier">full</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="keyword">bool</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="special">(</span><span class="identifier">length</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">capacity</span><span class="special">()));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">length</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">capacity</span><span class="special">());</span>
    <span class="special">}</span>

    <span class="keyword">bool</span> <span class="identifier">empty</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="keyword">bool</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">length</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">));</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">length</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="keyword">int</span> <span class="identifier">length</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
    <span class="keyword">virtual</span> <span class="keyword">int</span> <span class="identifier">capacity</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>

    <span class="keyword">virtual</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">item</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>

    <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">push</span><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
    <span class="keyword">virtual</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">pop</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>

    <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">clear</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">int</span> <span class="identifier">abstract_stack</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">length</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
    <span class="keyword">int</span> <span class="identifier">result</span><span class="special">;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">result</span><span class="special">,</span> <span class="keyword">this</span><span class="special">)</span>
        <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">(</span><span class="keyword">int</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">result</span><span class="special">)</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span>
        <span class="special">})</span>
    <span class="special">;</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="keyword">false</span><span class="special">);</span>
    <span class="keyword">return</span> <span class="identifier">result</span><span class="special">;</span>
<span class="special">}</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">int</span> <span class="identifier">abstract_stack</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">capacity</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
    <span class="keyword">int</span> <span class="identifier">result</span><span class="special">;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">result</span><span class="special">,</span> <span class="keyword">this</span><span class="special">)</span>
        <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">(</span><span class="keyword">int</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">result</span><span class="special">)</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span>
        <span class="special">})</span>
    <span class="special">;</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="keyword">false</span><span class="special">);</span>
    <span class="keyword">return</span> <span class="identifier">result</span><span class="special">;</span>
<span class="special">}</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abstract_stack</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">item</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;&gt;</span> <span class="identifier">result</span><span class="special">;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">result</span><span class="special">,</span> <span class="keyword">this</span><span class="special">)</span>
        <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">empty</span><span class="special">());</span>
        <span class="special">})</span>
    <span class="special">;</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="keyword">false</span><span class="special">);</span>
    <span class="keyword">return</span> <span class="special">*</span><span class="identifier">result</span><span class="special">;</span>
<span class="special">}</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">abstract_stack</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">push</span><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span><span class="special">)</span> <span class="special">{</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="keyword">this</span><span class="special">)</span>
        <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">full</span><span class="special">());</span>
        <span class="special">})</span>
        <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">empty</span><span class="special">());</span>
        <span class="special">})</span>
    <span class="special">;</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="keyword">false</span><span class="special">);</span>
<span class="special">}</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">abstract_stack</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">pop</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span><span class="special">)</span> <span class="special">{</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;&gt;</span> <span class="identifier">result</span><span class="special">;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">old_ptr</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">old_item</span> <span class="special">=</span> <span class="identifier">BOOST_CONTRACT_OLDOF</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">item</span><span class="special">());</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">result</span><span class="special">,</span> <span class="keyword">this</span><span class="special">)</span>
        <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">empty</span><span class="special">());</span>
        <span class="special">})</span>
        <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">result</span><span class="special">)</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(!</span><span class="identifier">full</span><span class="special">());</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(*</span><span class="identifier">result</span> <span class="special">==</span> <span class="special">*</span><span class="identifier">old_item</span><span class="special">);</span>
        <span class="special">})</span>
    <span class="special">;</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="keyword">false</span><span class="special">);</span>
    <span class="keyword">return</span> <span class="special">*</span><span class="identifier">result</span><span class="special">;</span>
<span class="special">}</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">abstract_stack</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">clear</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span><span class="special">)</span> <span class="special">{</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="keyword">this</span><span class="special">)</span>
        <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">empty</span><span class="special">());</span>
        <span class="special">})</span>
    <span class="special">;</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="keyword">false</span><span class="special">);</span>
<span class="special">}</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">vstack</span>
    <span class="preprocessor">#define</span> <span class="identifier">BASES</span> <span class="keyword">private</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor_precondition</span><span class="special">&lt;</span> <span class="special">\</span>
            <span class="identifier">vstack</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="special">&gt;,</span> <span class="keyword">public</span> <span class="identifier">abstract_stack</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span>
    <span class="special">:</span> <span class="identifier">BASES</span>
<span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">typedef</span> <span class="identifier">BOOST_CONTRACT_BASE_TYPES</span><span class="special">(</span><span class="identifier">BASES</span><span class="special">)</span> <span class="identifier">base_types</span><span class="special">;</span>
    <span class="preprocessor">#undef</span> <span class="identifier">BASES</span>

    <span class="keyword">void</span> <span class="identifier">invariant</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">length</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">length</span><span class="special">()</span> <span class="special">&lt;</span> <span class="identifier">capacity</span><span class="special">());</span>
    <span class="special">}</span>

    <span class="identifier">BOOST_CONTRACT_OVERRIDES</span><span class="special">(</span><span class="identifier">length</span><span class="special">,</span> <span class="identifier">capacity</span><span class="special">,</span> <span class="identifier">item</span><span class="special">,</span> <span class="identifier">push</span><span class="special">,</span> <span class="identifier">pop</span><span class="special">,</span> <span class="identifier">clear</span><span class="special">)</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="keyword">explicit</span> <span class="identifier">vstack</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">count</span> <span class="special">=</span> <span class="number">10</span><span class="special">)</span> <span class="special">:</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor_precondition</span><span class="special">&lt;</span><span class="identifier">vstack</span><span class="special">&gt;([&amp;]</span> <span class="special">{</span>
            <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">count</span> <span class="special">&gt;=</span> <span class="number">0</span><span class="special">);</span>
        <span class="special">}),</span>
        <span class="identifier">vect_</span><span class="special">(</span><span class="identifier">count</span><span class="special">),</span> <span class="comment">// OK, executed after precondition so count &gt;= 0.</span>
        <span class="identifier">len_</span><span class="special">(</span><span class="number">0</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">length</span><span class="special">()</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">count</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">vstack</span><span class="special">()</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="comment">// Inherited from abstract_stack.</span>

    <span class="keyword">virtual</span> <span class="keyword">int</span> <span class="identifier">length</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="keyword">const</span> <span class="comment">/* override */</span> <span class="special">{</span>
        <span class="keyword">int</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">&lt;</span>
                <span class="identifier">override_length</span><span class="special">&gt;(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">result</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">vstack</span><span class="special">::</span><span class="identifier">length</span><span class="special">,</span> <span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">len_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="keyword">int</span> <span class="identifier">capacity</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span>
            <span class="keyword">const</span> <span class="comment">/* override */</span> <span class="special">{</span>
        <span class="keyword">int</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">&lt;</span>
                <span class="identifier">override_capacity</span><span class="special">&gt;(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">result</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">vstack</span><span class="special">::</span><span class="identifier">capacity</span><span class="special">,</span> <span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">vect_</span><span class="special">.</span><span class="identifier">size</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">item</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span>
            <span class="keyword">const</span> <span class="comment">/* override */</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;&gt;</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">&lt;</span>
                <span class="identifier">override_item</span><span class="special">&gt;(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">result</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">vstack</span><span class="special">::</span><span class="identifier">item</span><span class="special">,</span> <span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="special">*(</span><span class="identifier">result</span> <span class="special">=</span> <span class="identifier">vect_</span><span class="special">[</span><span class="identifier">len_</span> <span class="special">-</span> <span class="number">1</span><span class="special">]);</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">push</span><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">value</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span>
            <span class="comment">/* override */</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">&lt;</span>
                <span class="identifier">override_push</span><span class="special">&gt;(</span><span class="identifier">v</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">vstack</span><span class="special">::</span><span class="identifier">push</span><span class="special">,</span> <span class="keyword">this</span><span class="special">,</span> <span class="identifier">value</span><span class="special">);</span>
        <span class="identifier">vect_</span><span class="special">[</span><span class="identifier">len_</span><span class="special">++]</span> <span class="special">=</span> <span class="identifier">value</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">pop</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="comment">/* override */</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;&gt;</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">&lt;</span>
                <span class="identifier">override_pop</span><span class="special">&gt;(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">result</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">vstack</span><span class="special">::</span><span class="identifier">pop</span><span class="special">,</span> <span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="special">*(</span><span class="identifier">result</span> <span class="special">=</span> <span class="identifier">vect_</span><span class="special">[--</span><span class="identifier">len_</span><span class="special">]);</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">clear</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">virtual_</span><span class="special">*</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="comment">/* override */</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">&lt;</span>
                <span class="identifier">override_clear</span><span class="special">&gt;(</span><span class="identifier">v</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">vstack</span><span class="special">::</span><span class="identifier">clear</span><span class="special">,</span> <span class="keyword">this</span><span class="special">);</span>
        <span class="identifier">len_</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">vect_</span><span class="special">;</span>
    <span class="keyword">int</span> <span class="identifier">len_</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">vstack</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">s</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">==</span> <span class="number">3</span><span class="special">);</span>

    <span class="identifier">s</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">123</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">length</span><span class="special">()</span> <span class="special">==</span> <span class="number">1</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">pop</span><span class="special">()</span> <span class="special">==</span> <span class="number">123</span><span class="special">);</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_contract.examples.__cline90___calendar__a_very_simple_calendar"></a><a class="link" href="examples.html#boost_contract.examples.__cline90___calendar__a_very_simple_calendar" title="[Cline90] Calendar: A very simple calendar">[Cline90]
      Calendar: A very simple calendar</a>
</h3></div></div></div>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">contract</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>

<span class="keyword">class</span> <span class="identifier">calendar</span> <span class="special">{</span>
    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">access</span><span class="special">;</span>

    <span class="keyword">void</span> <span class="identifier">invariant</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">month</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="number">1</span><span class="special">);</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">month</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="number">12</span><span class="special">);</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">date</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="number">1</span><span class="special">);</span>
        <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">date</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="identifier">days_in</span><span class="special">(</span><span class="identifier">month</span><span class="special">()));</span>
    <span class="special">}</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="identifier">calendar</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">month_</span><span class="special">(</span><span class="number">1</span><span class="special">),</span> <span class="identifier">date_</span><span class="special">(</span><span class="number">31</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">constructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">month</span><span class="special">()</span> <span class="special">==</span> <span class="number">1</span><span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">date</span><span class="special">()</span> <span class="special">==</span> <span class="number">31</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">calendar</span><span class="special">()</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">destructor</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
    <span class="special">}</span>

    <span class="keyword">int</span> <span class="identifier">month</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">month_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">int</span> <span class="identifier">date</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="comment">// Check invariants.</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="identifier">date_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">void</span> <span class="identifier">reset</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">new_month</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">public_function</span><span class="special">(</span><span class="keyword">this</span><span class="special">)</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">new_month</span> <span class="special">&gt;=</span> <span class="number">1</span><span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">new_month</span> <span class="special">&lt;=</span> <span class="number">12</span><span class="special">);</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">month</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">new_month</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="identifier">month_</span> <span class="special">=</span> <span class="identifier">new_month</span><span class="special">;</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="keyword">static</span> <span class="keyword">int</span> <span class="identifier">days_in</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">month</span><span class="special">)</span> <span class="special">{</span>
        <span class="keyword">int</span> <span class="identifier">result</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">check</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">contract</span><span class="special">::</span><span class="identifier">function</span><span class="special">()</span>
            <span class="special">.</span><span class="identifier">precondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">month</span> <span class="special">&gt;=</span> <span class="number">1</span><span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">month</span> <span class="special">&lt;=</span> <span class="number">12</span><span class="special">);</span>
            <span class="special">})</span>
            <span class="special">.</span><span class="identifier">postcondition</span><span class="special">([&amp;]</span> <span class="special">{</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">&gt;=</span> <span class="number">1</span><span class="special">);</span>
                <span class="identifier">BOOST_CONTRACT_ASSERT</span><span class="special">(</span><span class="identifier">result</span> <span class="special">&lt;=</span> <span class="number">31</span><span class="special">);</span>
            <span class="special">})</span>
        <span class="special">;</span>

        <span class="keyword">return</span> <span class="identifier">result</span> <span class="special">=</span> <span class="number">31</span><span class="special">;</span> <span class="comment">// For simplicity, assume all months have 31 days.</span>
    <span class="special">}</span>

    <span class="keyword">int</span> <span class="identifier">month_</span><span class="special">,</span> <span class="identifier">date_</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
    <span class="identifier">calendar</span> <span class="identifier">cal</span><span class="special">;</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">cal</span><span class="special">.</span><span class="identifier">date</span><span class="special">()</span> <span class="special">==</span> <span class="number">31</span><span class="special">);</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">cal</span><span class="special">.</span><span class="identifier">month</span><span class="special">()</span> <span class="special">==</span> <span class="number">1</span><span class="special">);</span>

    <span class="identifier">cal</span><span class="special">.</span><span class="identifier">reset</span><span class="special">(</span><span class="number">8</span><span class="special">);</span> <span class="comment">// Set month </span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">cal</span><span class="special">.</span><span class="identifier">month</span><span class="special">()</span> <span class="special">==</span> <span class="number">8</span><span class="special">);</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2008-2019 Lorenzo Caminiti<p>
        Distributed under the Boost Software License, Version 1.0 (see accompanying
        file LICENSE_1_0.txt or a copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="extras.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../reference.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
